This example notebook shows the code we used to download twitter feeds for the in-class assignment. You can try to follow along but this notebook may not work on some systems.
Before starting we need to make sure Tweepy module is installed. You need to use the pip command to install this module (other common install commands include conda, or easy_install. which one you use depends on the module). More information about Tweepy can be found on their website:
You only need to run the installer once on your system.
In [ ]:
!pip install tweepy
Assuming the installaiton wroked. You can now import the tweepy module.
In [ ]:
import tweepy
The next step is to get a costomer_key, consumer_secret, access_token, and access_token_secret from your twitter account. This is not strait forward but fortunalty you should only need to do it once. Here are the basic steps:
In [ ]:
consumer_key='putstringhere'
consumer_secret = 'putstringhere'
access_token = 'putstringhere'
access_token_secret = 'putstringhere'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
Now we can check that it is working by pulling the timeline from your personal twitter feed
In [ ]:
public_tweets = api.home_timeline()
for tweet in public_tweets:
print(tweet.text)
What we really want are the feeds from the presidential candidates. You need to search for the tweeter feed handle for each person. Put their name in the search menu and go to their twiter timeline. Their feed name is in the URL. For example here is barack obama's timeline:
https://twitter.com/barackobama
His twitter screen_name is just barackobama.
Now lets download 10 of his latest twitters using the user_timeline function
In [ ]:
#Test the code
public_tweets = api.user_timeline(screen_name = 'barackobama', count = 10, include_rts = True)
f = open('barackobama_tweets.txt', 'w')
for tweet in public_tweets:
f.write(tweet.text+'\n')
f.close()
In [ ]:
twitter_names = [ 'BarackObama', 'realDonaldTrump','HillaryClinton', 'BernieSanders', 'tedcruz']
for name in twitter_names:
print(name)
public_tweets = api.user_timeline(screen_name = name, count = 1400, include_rts = True)
f = open(name+'_tweets.txt', 'w')
for tweet in public_tweets:
f.write(tweet.text+'\n')
f.close()
For class, I uploaded these files to my MSU account.
In [ ]: