I wish to obtain tweets of a particular hashtag from a a particular location say Chennai for analysing data. I'm really new to Twitter API and tweepy. I found that the search url would look like this : https://api.twitter.com/1.1/search/tweets.json?q=%23cricket&geocode=-22.912214,-43.230182,1km&lang=pt&result_type=recent
How do the same in tweepy ? Code so far :
import tweepy
ckey = ""
csecret = ""
atoken = ""
asecret = ""
OAUTH_KEYS = {'consumer_key':ckey, 'consumer_secret':csecret,
'access_token_key':atoken, 'access_token_secret':asecret}
auth = tweepy.OAuthHandler(OAUTH_KEYS['consumer_key'], OAUTH_KEYS['consumer_secret'])
api = tweepy.API(auth)
cricTweet = tweepy.Cursor(api.search, q='cricket').items(10)
for tweet in cricTweet:
print tweet.created_at, tweet.text, tweet.lang
Step-by-step Approach:Create an explicit function to display tweet data. Create another function to scrape data regarding a given Hashtag using tweepy module. In the Driver Code assign Twitter Developer account credentials along with the Hashtag, initial date and number of tweets.
Go to twitter.com. In the Search bar in the top right-hand corner, type what you want to search, then press Enter. Go to Search filters in the top right and check the circle to the right of Near you, under Location.
Identifying the location in the GUI : In order to get the location we have to do the following : Identify the user ID or the screen name of the profile. Get the User object of the profile using the get_user() method with the user ID or the screen name. From this object, fetch the location attribute present in it.
Twitter enables users to specify a location for individual Tweets. PowerTrack offers multiple ways to filter for Tweets by Tweet-specific location data through its various operators (see our documentation for Twitter PowerTrack Operators for details).
You need to use the geocode
parameter in Tweepy. Using the lat/long/radius from your search URL, your cursor should be defined like so:
tweepy.Cursor(api.search, q='cricket', geocode="-22.9122,-43.2302,1km").items(10)
See the Tweepy API.search
documentation for more information on the parameters you can include in your search.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With