I recently met someone who is interested in mining data from Twitter. In addition to mining data from Twitter however, they’re also interested in collecting all of the replies. I thought that I would try giving it a shot and sharing what I learn.
Note: This post assumes that Python is installed on your computer. If you haven’t installed Python, this Python Wiki walks you through the process.
pip install tweepy
*Note: If your environments are configured like mine, you may need to type: conda install -c conda-forge tweepy
Now, go to Twitter’s developer page to register your app (you will have to sign in with your username and password, or sign up with a new username). You should see a button on the right-hand side of the page that says “Create New App.” Fill out the necessary fields (i.e. name of the app; it’s description; your website) and then check the box that says you agree to their terms, which I linked to above. If you don’t have a publicly accessible website, just list the web address that is hosting your app (e.g., link to your school profile; link to your work website). You can likely ignore the Callback URL field, unless you are allowing users to log into your app to authenticate themselves. In which case, enter the URL where they would be returned after they’ve given permission to Twitter to use your app.
After registering your app, you should see a page where you can create your access token. Click the “Create my access token” button. If you don’t see this button after a few seconds, refresh the page. The next page will ask you what type of access you need. For this example, we will need Read, Write, and Access Direct Messages. Now, note your OAuth settings, particularly your Consumer Key, Consumer Secret, OAuth Access Token, and OAuth Access Token Secret. Don’t share this information with anyone!
Next, import tweepy and use the OAuth interface to collect data.
import tweepy from tweepy import OAuthHandler consumer_key = 'YOUR-CONSUMER-KEY' consumer_secret = 'YOUR-CONSUMER-SECRET' access_token = 'YOUR-ACCESS-TOKEN' access_secret = 'YOUR-ACCESS-SECRET' auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_secret) api = tweepy.API(auth, wait_on_rate_limit=True)
To collect tweets that are currently being shared on Twitter. For example, tweets that are under “StarwarsDay”. Note that I’ve asked Python to format the tweets by first listing the the user and screen name, and then their tweet. If you want ALL the meta data, remove the specifications that I wrote.
#you'll need to import json to run this script import json class PrintListener(tweepy.StreamListener): def on_data(self, data): # Decode the JSON data tweet = json.loads(data) # Print out the Tweet print('@%s: %s' % (tweet['user']['screen_name'], tweet['text'].encode('ascii', 'ignore'))) def on_error(self, status): print(status) if __name__ == '__main__': listener = PrintListener() # Show system message print('I will now print Tweets containing "StarWarsDay"! ==>') # Authenticate auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_secret) # Connect the stream to our listener stream = tweepy.Stream(auth, listener) stream.filter(track=['StarWarsDay'], async=True)
Here’s a brief glimpse of what I got:
I will now print Tweets containing "StarWarsDay"! ==> @Amanda33441401: b'RT @givepennyuk: This #StarWarsDay we want you to feel inspired by the force to think up a Star Wars fundraiser!\n\n Star Wars Movie Marat' @jin_keapjjang: b'RT @HamillHimself: People around the world are marking #StarWarsDay in spectacular style. #MayThe4thBeWithYou https://t.co/BM02D965Xa via @' @Bradleyg1996G: b'RT @NHLonNBCSports: MAY THE PORGS BE WITH YOU\n\n#StarWarsDay #MayThe4thBeWithYou https://t.co/HWAmptYND5' @thays_jeronimo: b"RT @g1: 'May the 4th' celebrado por fs de 'Star Wars' https://t.co/ggNhaEQCPV #MayThe4thBeWithYou #StarWarsDay #G1 https://t.co/wUY74DZL" @DF_SomersetKY: b"If you're a fan of the franchise, you're going to love all of this Star Wars gear for your car! Tweet us your favor https://t.co/PteAtqS1Ui" @zakrhssn: b'RT @williamvercetti: #StarWarsDay https://t.co/fgHZzTZ0Fm' @kymaticaa: b'RT @Electric_Forest: May The Forest be with you. #ElectricForest #StarWarsDay #StarWars https://t.co/bfQnZHI8eX' @hullodave: b'"Only Imperial Stormtroopers are this precise" How precise? Not very? But why? Science! #StarWarsDay https://t.co/niZ2h6ssnp'
To store the data you just collected, rather than printing it, you’ll have to add some extra code (see text in red)
import json class PrintListener(tweepy.StreamListener): def on_data(self, data): # Decode the JSON data tweet = json.loads(data) # Print out the Tweet print('@%s: %s' % (tweet['user']['screen_name'], tweet['text'].encode('ascii', 'ignore'))) with open('StarWarsDay.csv','a') as f: f.write(data) except: pass def on_error(self, status): print(status) if __name__ == '__main__': listener = PrintListener() # Show system message print('I will now print Tweets containing "StarWarsDay"! ==>') # Authenticate auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_secret) # Connect the stream to our listener stream = tweepy.Stream(auth, listener) stream.filter(track=['StarWarsDay'], async=True)
Now, let’s see if it’s possible to get replies to a tweet. I checked these users (listed above) and none of them have any replies. So I decided to just search #StarWarsDay on Twitter instead. I immediately found the twitter handle for Arrested Development, which has replies (at the time of this writing 9) to their tweet that includes #StarWarsDay:
Look at the hyperlink: [“https://twitter.com/bluthquotes/status/992433028155654144”%5D
You can see that the user we are interested in is @bluthquotes and that the id for this particular tweet is “992433028155654144”
To get tweets from just @bluthquotes, you would type
bluthquotes_tweets = api.user_timeline(screen_name = 'bluthquotes', count = 100) for status in bluthquotes_tweets: print(status)
To see all the replies to @bluthquotes tweet posted above:
replies= non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd) for full_tweets in tweepy.Cursor(api.user_timeline,screen_name='bluthquotes',timeout=999999).items(10): for tweet in tweepy.Cursor(api.search,q='to:bluthquotes', since_id=992433028155654144, result_type='recent',timeout=999999).items(1000): if hasattr(tweet, 'in_reply_to_status_id_str'): if (tweet.in_reply_to_status_id_str==full_tweets.id_str): replies.append(tweet.text) print("Tweet :",full_tweets.text.translate(non_bmp_map)) for elements in replies: print("Replies :",elements) replies.clear()
*Note: change the .items(10) line to get more replies. Remember that Twitter limits you to 100 per hour (at least at the time of this writing).
This is what I got:
Tweet : @HulkHogan You ok hermano? Tweet : Go see a Star War #MayTheForceBeWithYou #StarWarsDay https://t.co/OLcmCAEl30 Replies : @bluthquotes @mlot11 Replies : @bluthquotes Oh my yes Replies : @bluthquotes Star Wars needs more gay characters #jarjarXobama Replies : @bluthquotes @TheSAPeacock May the 4th be with you!!!! Replies : @bluthquotes @SaraAnneGill Replies : @bluthquotes @kurkobains Por q xuxa la sacaron de #Netflix Replies : @bluthquotes https://t.co/Qv4KJJ6dFU Replies : @bluthquotes @hiagorecanello Replies : @bluthquotes No it’s #CincoDeCuatro Replies : @bluthquotes @auburnhays 😂 Replies : @bluthquotes Go see a Star War on Cinco de Quatro! 🤠🌶️🍹 Replies : @bluthquotes @jmdroberts Tweet : RT @arresteddev: Hey, hermanos! It's Cinco de Cuatro! Season 4 Remix is now streaming. https://t.co/Alw0Z2Zwlm Tweet : Keep fighting little guy #StarWarsDay #MayThe4thBeWithYou https://t.co/Uim4D2BP49 Replies : @bluthquotes "worth every penny" Replies : @bluthquotes You’re still doin’ that? Replies : @bluthquotes Im crying 😭 Replies : @bluthquotes @theJdog 😂😂😂😂😂 Tweet : @JTHM8008 @herooine @JeffEisenband @ZachAJacobson I say HUZZAH! like this at least 5 times a week. Tweet : @herooine @JeffEisenband @ZachAJacobson Checks out ✅ Tweet : @gjb512 It’s there already. Huzzah! Tweet : @drkatiemd_ @MitchHurwitz It’s a wonderful program! Tweet : I prematurely blue myself #EmbarrassmentIn4Words https://t.co/QYUFeSKFT2 Tweet : @sebastrivi @VICE @arresteddev I’m not on board
You can see that it’s not quite what I wanted, which is just the responses to the Star Wars tweet (see red text). According to the API reference page, there should be a way to limit the returned text to the replies to the specific tweet we are interested, but I will have to continue tinkering with it. I’ll post an update when I figure it out.