Skip to content

Instantly share code, notes, and snippets.

@aditideokar93
Last active June 26, 2020 20:45
Show Gist options
  • Save aditideokar93/ee3c4050821170a590d8ef9c6c8328a5 to your computer and use it in GitHub Desktop.
Save aditideokar93/ee3c4050821170a590d8ef9c6c8328a5 to your computer and use it in GitHub Desktop.
Fetch older tweets using GetOldTweets3 package
__author__ = "Aditi Deokar"
"""
The Python code below makes use of GetOldTweets3 Package to collect older tweets
"""
import time
import pandas as pd
import GetOldTweets3 as got
#list of column names
COLS = ['id', 'created_at','original_text', 'screen_name', 'author_id',
'replies', 'retweet_count', 'to','hashtags',
'user_mentions', 'urls']
path=r"D:\Older_tweets_data"
politicians=['SenatorMenendez','SenStabenow','SenatorTester','SenWhitehouse','SenatorDurbin']
#Function to fetch the tweet object, grab the information and write into a .csv file
for screenname in politicians:
entry = []
tweetCriteria = got.manager.TweetCriteria().setUsername(screenname)\
.setSince("2019-12-18")\
.setUntil("2020-01-01")\
.setMaxTweets(1000)\
.setEmoji("unicode")
tweet = got.manager.TweetManager.getTweets(tweetCriteria)
for twt in tweet:
entry.append([twt.id,twt.formatted_date,twt.text,twt.username,twt.author_id,twt.replies,twt.retweets,twt.to,twt.hashtags,twt.mentions,twt.urls])
print(len(entry),screenname)
df = pd.DataFrame(entry,columns=COLS)
# print(df.head())
df.to_csv(path+'\\'+screenname+"_tweets.csv", mode='a+', columns=COLS, index=False,
encoding="utf-8")
time.sleep(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment