-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
86 lines (55 loc) · 1.88 KB
/
scraper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
# Configuring Browser
#---------------------------------------------------------
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--log-level=3")
browser = webdriver.Chrome(options=chrome_options)
browser.set_window_size(1080, 960)
# FILL IN THE BLANKS
#---------------------------------------------------------
def find_subreddit(subreddit):
"""Game Plan:
- Navigates to Reddit
- Searches for the subreddit
- Clicks on link to subreddit
Args:
subreddit (str): the subreddit to be visited
"""
# TODO: implement this
def get_titles():
"""Game Plan:
- Choose how you want to find the title elements
- e.g by class name, tag name, xpath, etc
- Use browser.find_elements(.........)
- Convert each element in the list into text
Returns:
titles (list): a list of titles of posts found in the subreddit
"""
# TODO: implement this
titles = []
return titles
def display(titles_to_display):
"""Game Plan:
- Display your results in a cute format <3
Args:
titles_to_display (list): the titles to be displayed cutely
"""
# TODO: optional implementation
# RUN IT
#---------------------------------------------------------
def run(subreddit):
"""Puts it all together!
N.B. Since our browser is a global variable we're not concerned
about having to pass it around function to function
Args:
subreddit (str): the subreddit we wish to scrape
"""
find_subreddit(subreddit)
subreddit_titles = get_titles()
display(subreddit_titles)
# Uncomment when you're ready. Peer pressure is lame, so no rush <3
# run("Beans")