Shashank Tiwari
Shashank Tiwari

Reputation: 31

Applying filter to extract tweets on some hashtags from a particular country using python

I'm using the below python script to get tweets on #lockdownindia, #lockdownextension and #covid19 to get the tweets from my country (INDIA) and perform sentiment analysis. I've used cursor() from tweepy library to do so. When I use #geocode: it helps me out, but due to inaccurate radius, I'm getting tweets from neighboring countries like Pakistan as well, which I don't want. While reading Twitter documentation, I came to know about place_country: but it is not working. It is returning empty dataframe. Any help on how to use place_country: will be appreciated.
Also, is it possible to get all the attributes of a tweet in a single go, as happens while using streaming API

consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token = (access_token, access_token_secret)

api = tweepy.API(auth, wait_on_rate_limit=True)

query = '#lockdownindia OR #lockdownextension OR #covid19 -filter:retweets AND place_country:IN'     #['-filter:retweets place_country:IN']        #geocode:20.5937,78.9629,910mi
max_tweets = 100

tweets = tweepy.Cursor(api.search, q=query, since = "2020-06-05", until = "2020-06-21",lang="en").items(max_tweets)

Upvotes: 1

Views: 623

Answers (2)

omar-h-omar
omar-h-omar

Reputation: 84

Hey sorry I couldn't respond to this earlier. First off you'd want to import some libraries which are json, requests and time using the following line.

import json, requests, time

These should help you make http requests to the twitter api and manipulate the JSON response. Next you'd want to set your http request. I would suggest setting a variable for this. You can have it like this.

myrequest = 'https://api.twitter.com/1.1/search/tweets.json?q=from%3Atwitterdev&result_type=mixed&count=2'

Lastly you'd make the GET request to the twitter API. I am gonna save my request to the variable response.

response = requests.request(method = "GET", url = myrequest)

Now if you will have your response from the server. To access the JSON content of that response you need to use this command. I am gonna save my JSON content to the variable result.

result = json.loads(response.content)

Now you should have the JSON content and you can look inside of it just like you would've with a dictionary. I haven't used the twitter API before but I hope this helps. This is what I would use with other APIs.

Sidenote: Here is a link to how your http request should be from twitter. Best of Luck :)

Upvotes: 1

omar-h-omar
omar-h-omar

Reputation: 84

Hey there I have checked the documentation and it seems like you are not supposed to put place-country in your query. It's one of the attributes you will be able to find in the JSON reply from Twitter. I suggest checking their JSON message under the place attribute for country and using a loop check if it's set to India.

Upvotes: 2

Related Questions