-
-
Save suraj-deshmukh/b318ced868a14dc0fa1a to your computer and use it in GitHub Desktop.
import pandas as pd #to store tweets into csv | |
import tweepy | |
#Twitter API credentials | |
consumer_key = "" | |
consumer_secret = "" | |
access_key = "" | |
access_secret = "" | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_key, access_secret) | |
api = tweepy.API(auth) | |
def get_all_tweets(screen_name): | |
alltweets = [] | |
new_tweets = api.user_timeline(screen_name = screen_name,count=200) | |
alltweets.extend(new_tweets) | |
oldest = alltweets[-1].id - 1 | |
while len(new_tweets) > 0: | |
print "getting tweets before %s" % (oldest) | |
new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest) | |
alltweets.extend(new_tweets) | |
oldest = alltweets[-1].id - 1 | |
print "...%s tweets downloaded so far" % (len(alltweets)) | |
data=[[obj.user.screen_name,obj.user.name,obj.user.id_str,obj.user.description.encode("utf8"),obj.created_at.year,obj.created_at.month,obj.created_at.day,"%s.%s"%(obj.created_at.hour,obj.created_at.minute),obj.id_str,obj.text.encode("utf8")] for obj in alltweets ] | |
dataframe=pd.DataFrame(data,columns=['screen_name','name','twitter_id','description','year','month','date','time','tweet_id','tweet']) | |
dataframe.to_csv("%s_tweets.csv"%(screen_name),index=False) | |
if __name__ == '__main__': | |
#pass in the username of the account you want to download | |
get_all_tweets("TimesNow") |
Same problem here.
#write the csv
with open('%s_tweets.csv' % screen_name, 'w') as f:
writer = csv.writer(f)
writer.writerow(["id","created_at","text"])
writer.writerows(outtweets)
error: NameError: name 'screen_name' is not defined
Q: do we need to manually replace the csv file locations with a hard-coded directory path?
Some of the indentations needed to be adjusted (lines 27-29), but then it ran fine.
I didn't need to hard-code a directory path -- the csv file was created in the same location as the python file itself.
FYI -- if you are running Python 3 -- the print commands will need a parentheses after print and at the end of the line, so
print "getting tweets before %s" % (oldest)
becomes
print ("getting tweets before %s" % (oldest))
Thanks for the file :)
Hi there thanks for the code.
Almost all went fine, except this error. Do you have any idea? I am using python 3.5
NameError Traceback (most recent call last)
in ()
1 dataframe.to_csv("%s_tweets.csv"%(data),index=False)
----> 2 dataframe.to_csv("%s_tweets.csv"%(screen_name),index=False)
NameError: name 'screen_name' is not defined