Skip to content

Instantly share code, notes, and snippets.

@jamespaultg
Created March 5, 2020 02:35
Show Gist options
  • Save jamespaultg/ec6cd358eaabb55d5e44855abafe35a9 to your computer and use it in GitHub Desktop.
Save jamespaultg/ec6cd358eaabb55d5e44855abafe35a9 to your computer and use it in GitHub Desktop.
Get tweets from twitter using the tweepy api
# Thanks to Kaggle user yassinehamdaoui1
# https://www.kaggle.com/c/nlp-getting-started/discussion/132762
import pandas as pd
import tweepy as tw
consumer_key = "put here you consumer_key"
consumer_secret ="put here your consumer_secret"
access_token = "your access"
access_token_secret ="your access token"
auth = tw.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth)
search_words ="earthquake+disaster" # you gonna search by hashtags , the "+" stands for concatenating keywords
date = "2020-01-01" # choose any date with the format "yyyy-mm-dd"
number_posts = 100 # choose the number of posts available in that day
posts =[]
tweets = tw.Cursor(api.search,q=search_words,lang="en",since=date).items(number_posts)
for tweet in tweets:
posts.append([tweet.user.screen_name,tweet.user.location,tweet.text])
df = pd.DataFrame(posts)
df.to_csv('data.csv',index=False,header=["username","location","text"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment