Skip to content

Instantly share code, notes, and snippets.

@mGalarnyk
Last active December 22, 2022 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mGalarnyk/5b251ea41c3626d6965ac7d5006da432 to your computer and use it in GitHub Desktop.
Save mGalarnyk/5b251ea41c3626d6965ac7d5006da432 to your computer and use it in GitHub Desktop.
Get more than 100 Tweets at a time using paginator. If you need more than 100 Tweets, you will have to use paginator (https://docs.tweepy.org/en/latest/pagination.html). Replace the limit=1000 with the maximum number of tweets you want.
# 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 wantretweets
# lang:en is asking for the tweets to be in english
query = '#petday -is:retweet lang:en'
tweets = tweepy.Paginator(client.search_recent_tweets, query=query,
tweet_fields=['context_annotations', 'created_at'], max_results=100).flatten(limit=1000)
for tweet in tweets:
print(tweet.text)
if len(tweet.context_annotations) > 0:
print(tweet.context_annotations)
@lamiabendebane
Copy link

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 wantretweets

lang:en is asking for the tweets to be in english

query = '#petday -is:retweet lang:en'
tweets = tweepy.Paginator(client.search_recent_tweets, query=query,
tweet_fields=['context_annotations', 'created_at'], max_results=100).flatten(limit=1000)

for tweet in tweets:
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