Import Tweet Data Using Twitter API & Tweepy
Python
An example of using Tweepy and the Twitter API to scrape tweet data. In this example the latest 500 tweets from London with the news hashtag are pulled. The location is derived from passing the coordinates of the centre of London and a 25km radius into the geocode parameter.
1| import tweepy 2| import pandas as pd 3| 4| #Declare keys & tokens and open access (generate own keys using twitter developer account) 5| consumer_key = "XXXXXXXX" 6| consumer_secret = "XXXXXXXX" 7| access_token = "XXXXXXXXXX" 8| access_token_secret = "XXXXXXXXXX" 9| auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 10| auth.set_access_token(access_token, access_token_secret) 11| api = tweepy.API(auth,wait_on_rate_limit=True) 12| 13| 14| text_query = '#news' 15| count = 500 16| 17| tweets = tweepy.Cursor(api.search, 18| q=text_query, 19| geocode='51.5074,-0.1278,25km', 20| result_type="recent", 21| lang="en").items(count) 22| fields = ['created_at', 23| 'id', 24| 'text', 25| 'location', 26| 'retweet_count', 27| 'friends_count', 28| 'followers_count', 29| 'coordinates', 30| 'favorite_count'] 31| 32| tweets_list = [[tweet.created_at, 33| tweet.id, 34| tweet.text, 35| tweet.user.location, 36| tweet.retweet_count, 37| tweet.user.friends_count, 38| tweet.user.followers_count, 39| tweet.coordinates, 40| tweet.favorite_count] for tweet in tweets] 41| 42| tweets_df = pd.DataFrame(tweets_list,columns=fields)
133
121
117
109