Skip to content

Instantly share code, notes, and snippets.

@mGalarnyk
Last active October 18, 2022 21:54
Show Gist options
  • Save mGalarnyk/37b6c10580f4dabbed1c760265344a3c to your computer and use it in GitHub Desktop.
Save mGalarnyk/37b6c10580f4dabbed1c760265344a3c to your computer and use it in GitHub Desktop.
Search for Tweets from the Last 7 Days using the tweepy API (OAuth2.0)
# OAuth2.0 Version
import tweepy
#Put your Bearer Token in the parenthesis below
client = tweepy.Client(bearer_token='Change this')
"""
If you don't understand search queries, there is an excellent introduction to it here:
https://github.com/twitterdev/getting-started-with-the-twitter-api-v2-for-academic-research/blob/main/modules/5-how-to-write-search-queries.md
"""
# Get tweets that contain the hashtag #petday
# -is:retweet means I don't want retweets
# lang:en is asking for the tweets to be in english
query = '#petday -is:retweet lang:en'
tweets = client.search_recent_tweets(query=query, tweet_fields=['context_annotations', 'created_at'], max_results=100)
"""
What context_annotations are:
https://developer.twitter.com/en/docs/twitter-api/annotations/overview
"""
for tweet in tweets.data:
print(tweet.text)
if len(tweet.context_annotations) > 0:
print(tweet.context_annotations)
@williammabotjadev
Copy link

Do you need Academic Research Level to access search_recent_tweets?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment