Please, in python3 and with tweepy is it possible to search for tweets only from a certain country and include other types of search?
In the example below I try to search for tweets from Mexico, in Spanish, since 2022-01-01, filtering retweets, and with the terms in the same tweet (activistas+ambientales+criminales)
But returns empty Does anyone know what could be wrong?
import tweepy
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)
places = api.geo_search(query="Mexico", granularity="country")
place_id = places[0].id
place_id
'25530ba03b7d90c6'
new_search = "place:%s AND activistas+ambientales+criminales -filter:retweets" % (place_id)
tweets = tweepy.Cursor(api.search,
q=new_search,
lang="es",
since='2022-01-01').items(100)
Edited 7/9/2022 after Mickael Martinez's reply:
Now I got the tweets! Thanks! I just want to know if the few cases returned are normal:
# I upgrade before
!pip install --upgrade tweepy
import pandas as pd
import tweepy
api_key = ''
api_key_secret = ''
bearer_token = ''
client = tweepy.Client(bearer_token)
# In this query I search for two words + that the tweet is in Spanish + that it is in Mexico + that it is not a retweet
query = "activistas ambientales lang:es place_country:mx -is:retweet"
# I ask to fetch from the beginning of 2022
# And delimit more tweets and user fields
response = client.search_all_tweets(query,
start_time = "2022-01-01T00:00:00Z",
tweet_fields=["id", "author_id", "text", "created_at", "attachments", "context_annotations", "entities", "geo"],
user_fields=["id", "name", "username", "created_at", "description"],
expansions='author_id'
)
tweets = response.data
# Save user data
users = {u["id"]: u for u in response.includes['users']}
# Create a dataframe with the data
my_demo_list = []
for tweet in tweets:
#print(tweet.id)
#print(tweet.text)
#print(tweet.geo)
# captures user data from the tweet that is in the iteration
author = tweet.author_id
#print(author)
for tweetu in response.data:
if users[tweetu.author_id]:
user = users[tweetu.author_id]
if user.id == author:
name = user.name
username = user.username
user_created_at = user.created_at
user_description = user.description
my_demo_list.append({'tweet_id': str(tweet.id),
'text': str(tweet.text),
'name': str(name),
'author_id': str(tweet.author_id),
'username': str(username),
'user_created_at': str(user_created_at),
'user_description': str(user_description),
'attachments': str(tweet.attachments),
'author_id': str(tweet.author_id),
'created_at': str(tweet.created_at),
'context_annotations': str(tweet.context_annotations),
'entities': str(tweet.entities),
'geo': str(tweet.geo)
})
all_tweets_found = pd.DataFrame(my_demo_list)
all_tweets_found.shape
(10, 12)
The place operator seems to be a premium operator in the Twitter V1.1 API (see the documentation here), so I'm not sure that you can use it in the standard search method.
Since you probably have an elevated access, you should update to the latest version of Tweepy and you may have access to the search_30_days
method. Maybe that you can use the place operator in it. If that does not work, I guess that you will have to pay for a premium access.
Edit after your comment:
Since you have an academic access, I would sugest you to:
pip install --upgrade tweepy
) because, based on your code, you seem to be using an outdated 3.X.X version.search_all_tweets
method in Tweepy (see here) where you can use place
and place_country
operators.query = ""
bearer_token = ""
client = tweepy.Client(bearer_token)
response = client.search_all_tweets(query)
print(response.data)
print(response.meta)
print(response.includes)
print(response.errors)
tweets = response.data
for tweet in tweets:
print(tweet.id)
print(tweet.text)
And you could read the documentation here to build the query.
But... do you really need to use Tweepy? Are you building an app for future users or are you just gathering data for your research? If the second answer is the good one, I would strongly suggest you to use Twarc, a command-line tool which is more suited for this purpose.