Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 283
anirudh_raja Avatar asked Dec 02 '14 07:12

anirudh_raja


People also ask

How do I get Tweets from hashtag Tweepy?

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.

How do I find Tweets from a specific location?

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.

How do I get Tweet location on Tweepy?

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.

Can you filter Twitter by location?

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).


1 Answers

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.

like image 66
Luigi Avatar answered Oct 18 '22 05:10

Luigi