Last active
October 18, 2022 21:54
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you need Academic Research Level to access search_recent_tweets?