This repository was archived by the owner on Oct 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathold_tweets.py
More file actions
45 lines (36 loc) · 1.46 KB
/
old_tweets.py
File metadata and controls
45 lines (36 loc) · 1.46 KB
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
#! /usr/bin/env python
from selenium import webdriver
from bs4 import BeautifulSoup
# Inputs:
# Dates should be in format YYYY-MM-DD.
def scrape(query, startDate, endDate):
#Initiate webdriver
url='https://twitter.com/search?q=' + query + ' since%3A' + startDate + ' until%3A' +endDate
driver = webdriver.Firefox()
driver.get(url)
# Use webdriver to load entire page through end of results
# Loop identifies bottom of page by seeing the same time stamp 10 times in a row.
sametimeNum = 0
lastTime = ""
while (sametimeNum<10):
timestamps=driver.find_elements_by_xpath("//li[@data-item-type='tweet']//div[@class='content']//small[@class='time']/a")
if timestamps[-1].get_attribute('title') == lastTime:
sametimeNum = sametimeNum+1
else:
sametimeNum = 0
lastTime = timestamps[-1].get_attribute('title')
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Load page into beautifulsoup and scrape
soup = BeautifulSoup(driver.page_source)
usernames = soup.find_all(class_='username js-action-profile-name')
timestamps = soup.find_all(class_='tweet-timestamp js-permalink js-nav js-tooltip')
tweets = soup.find_all(class_='TweetTextSize js-tweet-text tweet-text')
# Write to output list
tweetsOut=[]
for i in range(0,len(usernames)):
username=usernames[i].text
timestamp=timestamps[i].get('title')
tweet=str(tweets[i].text.encode("utf-8"))
tweetsOut.append([i, username, timestamp, tweet])
driver.quit()
return(tweetsOut)