[PR]
Examples
import tweepy
# Your app's API/consumer key and secret can be found under the Consumer Keys
# section of the Keys and Tokens tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps
consumer_key = ""
consumer_secret = ""
# Your account's (the app owner's account's) access token and secret for your
# app can be found under the Authentication Tokens section of the
# Keys and Tokens tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps
access_token = ""
access_token_secret = ""
auth = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret, access_token, access_token_secret
)
api = tweepy.API(auth)
# If the authentication was successful, this should print the
# screen name / username of the account
print(api.verify_credentials().screen_name)
import tweepy
# PIN-based OAuth
# https://developer.twitter.com/en/docs/authentication/oauth-1-0a/pin-based-oauth
# Your app's API/consumer key and secret can be found under the Consumer Keys
# section of the Keys and Tokens tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps
consumer_key = ""
consumer_secret = ""
auth = tweepy.OAuth1UserHandler(consumer_key, consumer_secret)
# This prints a URL that can be used to authorize your app
# After granting access to the app, a PIN to complete the authorization process
# will be displayed
print(auth.get_authorization_url())
# Enter that PIN to continue
verifier = input("PIN: ")
auth.get_access_token(verifier)
api = tweepy.API(auth)
# If the authentication was successful, this should print the
# screen name / username of the account
print(api.verify_credentials().screen_name)
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret, access_token, access_token_secret
)
api = tweepy.API(auth)
# Tweet / Update Status
# The app and the corresponding credentials must have the Write permission
# Check the App permissions section of the Settings tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps
# Make sure to reauthorize your app / regenerate your access token and secret
# after setting the Write permission
api.update_status("This Tweet was Tweeted using Tweepy!")
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret, access_token, access_token_secret
)
api = tweepy.API(auth)
# Follow every follower of the authenticated user
for follower in tweepy.Cursor(api.get_followers).items():
follower.follow()
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret, access_token, access_token_secret
)
# Setting wait_on_rate_limit to True when initializing API will initialize an
# instance, called api here, that will automatically wait, using time.sleep,
# for the appropriate amount of time when a rate limit is encountered
api = tweepy.API(auth, wait_on_rate_limit=True)
# This will search for Tweets with the query "Twitter", returning up to the
# maximum of 100 Tweets per request to the Twitter API
# Once the rate limit is reached, it will automatically wait / sleep before
# continuing
for tweet in tweepy.Cursor(api.search_tweets, "Twitter", count=100).items():
print(tweet.id)
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
# Subclass Stream to print IDs of Tweets received
class IDPrinter(tweepy.Stream):
def on_status(self, status):
print(status.id)
# Initialize instance of the subclass
printer = IDPrinter(
consumer_key, consumer_secret,
access_token, access_token_secret
)
# Filter realtime Tweets by keyword
printer.filter(track=["Twitter"])
import tweepy
# Your app's bearer token can be found under the Authentication Tokens section
# of the Keys and Tokens tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps
bearer_token = ""
# Your app's API/consumer key and secret can be found under the Consumer Keys
# section of the Keys and Tokens tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps
consumer_key = ""
consumer_secret = ""
# Your account's (the app owner's account's) access token and secret for your
# app can be found under the Authentication Tokens section of the
# Keys and Tokens tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps
access_token = ""
access_token_secret = ""
# You can authenticate as your app with just your bearer token
client = tweepy.Client(bearer_token=bearer_token)
# You can provide the consumer key and secret with the access token and access
# token secret to authenticate as a user
client = tweepy.Client(
consumer_key=consumer_key, consumer_secret=consumer_secret,
access_token=access_token, access_token_secret=access_token_secret
)
import tweepy
bearer_token = ""
client = tweepy.Client(bearer_token)
# Search Recent Tweets
# This endpoint/method returns Tweets from the last seven days
response = client.search_recent_tweets("Tweepy")
# The method returns a Response object, a named tuple with data, includes,
# errors, and meta fields
print(response.meta)
# In this case, the data field of the Response returned is a list of Tweet
# objects
tweets = response.data
# Each Tweet object has default ID and text fields
for tweet in tweets:
print(tweet.id)
print(tweet.text)
# By default, this endpoint/method returns 10 results
# You can retrieve up to 100 Tweets by specifying max_results
response = client.search_recent_tweets("Tweepy", max_results=100)
import tweepy
bearer_token = ""
client = tweepy.Client(bearer_token)
# You can specify additional Tweet fields to retrieve using tweet_fields
response = client.search_recent_tweets(
"Tweepy", tweet_fields=["created_at", "lang"]
)
tweets = response.data
# You can then access those fields as attributes of the Tweet objects
for tweet in tweets:
print(tweet.id, tweet.lang)
# Alternatively, you can also access fields as keys, like a dictionary
for tweet in tweets:
print(tweet["id"], tweet["lang"])
# There’s also a data attribute/key that provides the entire data dictionary
for tweet in tweets:
print(tweet.data)
import tweepy
bearer_token = ""
client = tweepy.Client(bearer_token)
# You can specify expansions to retrieve additional objects that relate to the
# returned results
response = client.search_recent_tweets(
"Tweepy", expansions=["attachments.media_keys", "author_id"]
)
tweets = response.data
# You can then access those objects in the includes Response field
includes = response.includes
users = includes["users"]
# The IDs that represent the expanded objects are included directly in the
# returned data objects
for tweet in tweets:
print(tweet.author_id)
# An efficient way of matching expanded objects to each data object is to
# create a dictionary of each type of expanded object, with IDs as keys
users = {user["id"]: user for user in users}
for tweet in tweets:
print(tweet.id, users[tweet.author_id].username)
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
client = tweepy.Client(
consumer_key=consumer_key, consumer_secret=consumer_secret,
access_token=access_token, access_token_secret=access_token_secret
)
# Create Tweet
# The app and the corresponding credentials must have the Write permission
# Check the App permissions section of the Settings tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps
# Make sure to reauthorize your app / regenerate your access token and secret
# after setting the Write permission
response = client.create_tweet(
text="This Tweet was Tweeted using Tweepy and Twitter API v2!"
)
print(f"https://twitter.com/user/status/{response.data['id']}")
import tweepy
bearer_token = ""
client = tweepy.Client(bearer_token)
# Get User's Tweets
# This endpoint/method returns Tweets composed by a single user, specified by
# the requested user ID
user_id = 2244994945
response = client.get_users_tweets(user_id)
# By default, only the ID and text fields of each Tweet will be returned
for tweet in response.data:
print(tweet.id)
print(tweet.text)
# By default, the 10 most recent Tweets will be returned
# You can retrieve up to 100 Tweets by specifying max_results
response = client.get_users_tweets(user_id, max_results=100)
import tweepy
bearer_token = ""
client = tweepy.Client(bearer_token)
# Get User's Mentions
# This endpoint/method returns Tweets mentioning a single user specified by the
# requested user ID
user_id = 2244994945
response = client.get_users_mentions(user_id)
# By default, only the ID and text fields of each Tweet will be returned
for tweet in response.data:
print(tweet.id)
print(tweet.text)
# By default, the 10 most recent Tweets will be returned
# You can retrieve up to 100 Tweets by specifying max_results
response = client.get_users_mentions(user_id, max_results=100)
import tweepy
bearer_token = ""
client = tweepy.Client(bearer_token)
# Get User's Liked Tweets
# This endpoint/method allows you to get information about a user’s liked
# Tweets
user_id = 2244994945
# By default, only the ID and text fields of each Tweet will be returned
# Additional fields can be retrieved using the tweet_fields parameter
response = client.get_liked_tweets(user_id, tweet_fields=["created_at"])
for tweet in response.data:
print(tweet.id, tweet.created_at)
import tweepy
bearer_token = ""
client = tweepy.Client(bearer_token)
# Get Tweet's Retweeters
# This endpoint/method allows you to get information about who has Retweeted a
# Tweet
tweet_id = 1460323737035677698
# By default, only the ID, name, and username fields of each user will be
# returned
# Additional fields can be retrieved using the user_fields parameter
response = client.get_retweeters(tweet_id, user_fields=["profile_image_url"])
for user in response.data:
print(user.username, user.profile_image_url)
import tweepy
bearer_token = ""
client = tweepy.Client(bearer_token)
# Get Tweet's Liking Users
# This endpoint/method allows you to get information about a Tweet’s liking
# users
tweet_id = 1460323737035677698
# By default, only the ID, name, and username fields of each user will be
# returned
# Additional fields can be retrieved using the user_fields parameter
response = client.get_liking_users(tweet_id, user_fields=["profile_image_url"])
for user in response.data:
print(user.username, user.profile_image_url)
import tweepy
bearer_token = ""
client = tweepy.Client(bearer_token)
# Get User's Followers
# This endpoint/method returns a list of users who are followers of the
# specified user ID
user_id = 2244994945
# By default, only the ID, name, and username fields of each user will be
# returned
# Additional fields can be retrieved using the user_fields parameter
response = client.get_users_followers(
user_id, user_fields=["profile_image_url"]
)
for user in response.data:
print(user.username, user.profile_image_url)
# By default, this endpoint/method returns 100 results
# You can retrieve up to 1000 users by specifying max_results
response = client.get_users_followers(user_id, max_results=1000)
import tweepy
bearer_token = ""
client = tweepy.Client(bearer_token)
# Get Tweets
# This endpoint/method returns a variety of information about the Tweet(s)
# specified by the requested ID or list of IDs
tweet_ids = [1460323737035677698, 1293593516040269825, 1293595870563381249]
# By default, only the ID and text fields of each Tweet will be returned
# Additional fields can be retrieved using the tweet_fields parameter
response = client.get_tweets(tweet_ids, tweet_fields=["created_at"])
for tweet in response.data:
print(tweet.id, tweet.created_at)
import tweepy
bearer_token = ""
client = tweepy.Client(bearer_token)
# Get Users
# This endpoint/method returns a variety of information about one or more users
# specified by the requested IDs or usernames
user_ids = [2244994945, 6253282]
# By default, only the ID, name, and username fields of each user will be
# returned
# Additional fields can be retrieved using the user_fields parameter
response = client.get_users(ids=user_ids, user_fields=["profile_image_url"])
for user in response.data:
print(user.username, user.profile_image_url)
import tweepy
bearer_token = ""
client = tweepy.Client(bearer_token)
# Get Recent Tweets Count
# This endpoint/method returns count of Tweets from the last seven days that
# match a search query
query = "Tweepy -is:retweet"
# Granularity is what you want the timeseries count data to be grouped by
# You can request minute, hour, or day granularity
# The default granularity, if not specified is hour
response = client.get_recent_tweets_count(query, granularity="day")
for count in response.data:
print(count)