31
loading...
This website collects cookies to deliver better user experience
pip3 install tweepy
pip3 install tweepy --upgrade
import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
search_recent_tweets
function available in Tweepy. You will have to pass it a search query to specify the data that you are looking for. In the example below, we are searching for Tweets from the last days from
the Twitter handle suhemparack
and we are excluding retweets using -is:retweet
.max_results
parameter. The maximum Tweets per request is 100.import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
# Replace with your own search query
query = 'from:suhemparack -is:retweet'
tweets = client.search_recent_tweets(query=query, tweet_fields=['context_annotations', 'created_at'], max_results=100)
for tweet in tweets.data:
print(tweet.text)
if len(tweet.context_annotations) > 0:
print(tweet.context_annotations)
search_all_tweets
method and pass it the search query. As shown in the example above, you can also specify additional Tweet fields etc. that you'd like returned for your request to the Twitter API.import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
# Replace with your own search query
query = 'from:suhemparack -is:retweet'
tweets = client.search_all_tweets(query=query, tweet_fields=['context_annotations', 'created_at'], max_results=100)
for tweet in tweets.data:
print(tweet.text)
if len(tweet.context_annotations) > 0:
print(tweet.context_annotations)
start_time
and end_time
parameters, as shown in the example below:import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
# Replace with your own search query
query = 'from:suhemparack -is:retweet'
# Replace with time period of your choice
start_time = '2020-01-01T00:00:00Z'
# Replace with time period of your choice
end_time = '2020-08-01T00:00:00Z'
tweets = client.search_all_tweets(query=query, tweet_fields=['context_annotations', 'created_at'],
start_time=start_time,
end_time=end_time, max_results=100)
for tweet in tweets.data:
print(tweet.text)
print(tweet.created_at)
import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
# Replace with your own search query
query = 'covid -is:retweet'
# Replace the limit=1000 with the maximum number of Tweets you want
for tweet in tweepy.Paginator(client.search_recent_tweets, query=query,
tweet_fields=['context_annotations', 'created_at'], max_results=100).flatten(limit=1000):
print(tweet.id)
file_name
with the a name of your chosing. If you wish to write other fields to the text file, make sure to adjust the script below accordingly.import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
# Replace with your own search query
query = 'covid -is:retweet'
# Name and path of the file where you want the Tweets written to
file_name = 'tweets.txt'
with open(file_name, 'a+') as filehandle:
for tweet in tweepy.Paginator(client.search_recent_tweets, query=query,
tweet_fields=['context_annotations', 'created_at'], max_results=100).flatten(
limit=1000):
filehandle.write('%s\n' % tweet.id)
search_recent_tweets
function.expansions
that we pass is author_id
. Additionally, we will specify the user_fields
that we want returned such as profile_image_url etc. Learn more about available fields here.includes
object. Then, we will look up the user information for each Tweet, using the author_id
of the Tweet.import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
# Replace with your own search query
query = 'covid -is:retweet'
tweets = client.search_recent_tweets(query=query, tweet_fields=['context_annotations', 'created_at'],
user_fields=['profile_image_url'], expansions='author_id', max_results=100)
# Get users list from the includes object
users = {u["id"]: u for u in tweets.includes['users']}
for tweet in tweets.data:
if users[tweet.author_id]:
user = users[tweet.author_id]
print(user.profile_image_url)
expansions
that we pass is attachments.media_keys
. Additionally, we will specify the media_fields
that we want returned such as preview_image_url etc. Learn more about available fields here.includes
object. Then, we will look up the media information for each Tweet, using the media_keys
from the attachements for the Tweet.import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
# Replace with your own search query
query = 'covid -is:retweet has:media'
tweets = client.search_recent_tweets(query=query, tweet_fields=['context_annotations', 'created_at'],
media_fields=['preview_image_url'], expansions='attachments.media_keys',
max_results=100)
# Get list of media from the includes object
media = {m["media_key"]: m for m in tweets.includes['media']}
for tweet in tweets.data:
attachments = tweet.data['attachments']
media_keys = attachments['media_keys']
print(tweet.text)
if media[media_keys[0]].preview_image_url:
print(media[media_keys[0]].preview_image_url)
place
, place_country
, bounding_box
, point_radius
etc. (these operators are currently only available in the academic research product track) that allow you to get these geo-tagged Tweets.US
, which is why we specify it in our search query using the place_country:US
operator. Now because by default, only the Tweet ID and Tweet text are returned, we have to specify the fact that we want the geo information in our response as well. We do this by passing the expansions='geo.place_id'
to the search_all_tweets
function and we also specify the geo fields that we are looking for using place_fields=['place_type','geo']
.includes
object, and we match on the place_id
to get the relevant geo information associated with the Tweet (the place.full_name
in the example below)import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
# Replace with your own search query
query = 'covid -is:retweet place_country:US'
tweets = client.search_all_tweets(query=query, tweet_fields=['context_annotations', 'created_at', 'geo'],
place_fields=['place_type', 'geo'], expansions='geo.place_id', max_results=100)
# Get list of places from includes object
places = {p["id"]: p for p in tweets.includes['places']}
for tweet in tweets.data:
print(tweet.id)
print(tweet.text)
if places[tweet.geo['place_id']]:
place = places[tweet.geo['place_id']]
print(place.full_name)
get_recent_tweets_count
function. You can pass it the search query
and specify the granularity
for the data aggregation e.g. if you want the daily count, hourly count or 15-minute count for a search term.import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
# Replace with your own search query
query = 'covid -is:retweet'
counts = client.get_recent_tweets_count(query=query, granuarity='day')
for count in counts.data:
print(count)
{'end': '2021-09-23T00:00:00.000Z', 'start': '2021-09-22T03:01:44.000Z', 'tweet_count': 142282}
get_users_tweets
method and pass it the user id. We can also specify additional fields that we want using the tweet_fields
and expansions
(as shown in the examples above).import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
# Replace user ID
id = '2244994945'
tweets = client.get_users_tweets(id=id, tweet_fields=['context_annotations','created_at','geo'])
for tweet in tweets.data:
print(tweet)
get_users_mentions
method and pass it the user id. We can also specify additional fields that we want using the tweet_fields
and expansions
(as shown in the examples above).import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
# Replace user ID
id = '2244994945'
tweets = client.get_users_mentions(id=id, tweet_fields=['context_annotations','created_at','geo'])
for tweet in tweets.data:
print(tweet)
get_users_followers
function and pass it the user ID. If we want additional fields for the User object such as profile_image_url
, we can specify those using the user_fields
parameter.import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
# Replace user ID
id = '2244994945'
users = client.get_users_followers(id=id, user_fields=['profile_image_url'])
for user in users.data:
print(user.id)
get_users_following
function and pass it the user ID. If we want additional fields for the User object such as profile_image_url
, we can specify those using the user_fields
parameter.import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
# Replace user ID
id = '2244994945'
users = client.get_users_following(id=id, user_fields=['profile_image_url'])
for user in users.data:
print(user.id)
get_liking_users
function and pass it the Tweet ID. If we want additional fields for the User object such as profile_image_url
, we can specify those using the user_fields
parameter.import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
# Replace Tweet ID
id = '1441054496931541004'
users = client.get_liking_users(id=id, user_fields=['profile_image_url'])
for user in users.data:
print(user)
get_retweeters
function and pass it the Tweet ID. If we want additional fields for the User object such as profile_image_url
, we can specify those using the user_fields
parameter.import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
# Replace Tweet ID
id = '1441054496931541004'
users = client.get_retweeters(id=id, user_fields=['profile_image_url'])
for user in users.data:
print(user)
get_liked_tweets
function and pass it the User ID. If we want additional fields for the Tweet object such as context_annotations
, created_at
etc. we can specify those using the tweet_fields
parameter.import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
# Replace User ID
id = '2244994945'
tweets = client.get_liked_tweets(id=id, tweet_fields=['context_annotations','created_at','geo'])
for tweet in tweets.data:
print(tweet)
get_tweets
function and pass it the list of Tweet IDs. By default, only the Tweet ID and Tweet text are returned. If we want additional Tweet fields, we can specify those using tweet_fields
parameter.import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
# Replace Tweet IDs
ids = ['1409935014725177344', '1409931481552543749', '1441054496931541004']
tweets = client.get_tweets(ids=ids, tweet_fields=['context_annotations','created_at','geo'])
for tweet in tweets.data:
print(tweet)
get_users
function and pass it the list of User IDs. If we want additional User fields, we can specify those using user_fields
parameter.import tweepy
client = tweepy.Client(bearer_token='REPLACE_ME')
# Replace User IDs
ids = ['2244994945']
users = client.get_users(ids=ids, user_fields=['profile_image_url'])
for user in users.data:
print(user.profile_image_url)
import tweepy
client = tweepy.Client(consumer_key='REPLACE_ME',
consumer_secret='REPLACE_ME',
access_token='REPLACE_ME',
access_token_secret='REPLACE_ME')
# Replace the text with whatever you want to Tweet about
response = client.create_tweet(text='hello world')
print(response)
import tweepy
client = tweepy.Client(consumer_key='REPLACE_ME',
consumer_secret='REPLACE_ME',
access_token='REPLACE_ME',
access_token_secret='REPLACE_ME')
# Replace the Tweet ID that you want to retweet
response = client.retweet(1409931481552543749)
print(response)
create_tweet
method, you can simply pass the in_reply_to_tweet_id
with the ID of the Tweet that you want to reply to, as shown below:import tweepy
client = tweepy.Client(consumer_key='REPLACE_ME',
consumer_secret='REPLACE_ME',
access_token='REPLACE_ME',
access_token_secret='REPLACE_ME')
# Replace the text with whatever you want to Tweet about
response = client.create_tweet(text='hello world reply', in_reply_to_tweet_id=12345678)
print(response)