-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedium-dawran6.py
53 lines (39 loc) · 1.46 KB
/
medium-dawran6.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
#!python3
"""
Modified sample code from:
https://medium.com/@dawran6/twitter-scraper-tutorial-with-python-requests-beautifulsoup-and-selenium-part-2-b38d849b07fe
See tutorial on installing the webdriver.
"""
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome()
base_url = "https://twitter.com/search?f=tweets&q="
query = "%40mamacityif"
url = "{}{}".format(base_url, query)
browser.get(url)
time.sleep(1)
body = browser.find_element_by_tag_name("body")
for _ in range(50):
body.send_keys(Keys.PAGE_DOWN)
time.sleep(0.2)
tweets = browser.find_elements_by_class_name("tweet-text")
for tweet in tweets:
print(tweet.text)
# Twitter API data is stored in a tweet tag and not used directly in the
# output for the user.
tweets = browser.find_elements_by_class_name("tweet")
for tweet in tweets:
t = tweet.find_element_by_class_name("_timestamp")
id_ = tweet.get_attribute("data-tweet-id")
path = tweet.get_attribute("data-permalink-path")
screen_name = tweet.get_attribute("data-screen-name")
print(t.text, screen_name, id_, path)
msg = tweet.find_element_by_class_name("TweetTextSize")
text = msg.text
print(text)
print("###################################")
# Quits anyway when script is over but stays open on crashes.
# Consider putting this in finally statement, with debug mode to not exit in case
# you want to check what the browser view contains.
browser.quit()