Hi all,
I want to extract tweets from a certain account during a specific time with some keywords.
But the twitter API keeps searching the most recent 200 tweets and then collect tweets with the keywords. I cannot find the perfect way to include user_id + start_date + keywords.
Can anyone help me?
tweets = api.user_timeline(id='MoDem', tweet_mode = 'extended', count=200)
simple_tweets = []
for tweet in tweets:
tweet_id = tweet.id_str
text = tweet.full_text
account = tweet.user.screen_name
created_at = tweet.created_at
tobject = time.strptime(str(created_at)[ :10],"%Y-%M-%d")
date = str(tobject.tm_year)+ "_" +str(tobject.tm_mon) + "_" +str(tobject.tm_mday)
tweet = {'id':tweet_id, 'text': text, 'account': account, 'date': date}
simple_tweets.append(tweet)
#immigration OR frontière OR immigrant
final_tweets = []
word1 = '[fF]rontière'
word2 = '[Ii]mmigration'
word3 = '[Ii]mmigrant'
for tweet in simple_tweets:
for word in tweet['text'].split():
if re.match(word1, word):
final_tweets.append(tweet)
elif re.match(word2, word):
final_tweets.append(tweet)
elif re.match(word3, word):
final_tweets.append(tweet)
print(final_tweets)
What code / library are you using? Because this appears to be code written for v1.1 API, while Academic Access is for v2 API
Either way, I recommend getting started with twarc:
Incidentally, on case you don’t have v2 Academic Access, twarc will also work for v1.1, see the docs linked in the post.