How to get tweets of a particular hashtag in a location in a tweepy?

anirudh_raja picture anirudh_raja · Dec 2, 2014 · Viewed 19.2k times · Source

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

Answer

Luigi picture Luigi · Dec 8, 2014

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.