Understanding and Implementing Tweepy Functions
for Twitter API
INTRODUCTION
The Role of Prediction in Decision Making
Predictions impact weather forecasting, financial markets, elections, fraud
detection, and disaster preparedness.
AI and big data enhance predictive accuracy, helping businesses and researchers
make informed decisions.
Data Mining & Twitter’s Role
Data mining uncovers valuable insights from vast datasets.
Sentiment analysis on Twitter can predict:
Election outcomes
Movie box office revenue
Marketing campaign success
Competitor weaknesses
Twitter API & Data Access
Search API → Retrieve past tweets.
Streaming API → Access real-time tweets.
Trends API → Track popular discussions.
NLP (Natural Language Processing) techniques help analyze tweet content.
Why Python for Twitter Analysis?
Powerful libraries allow complex tasks with minimal code.
Python's open-source community supports Twitter data mining.
Twitter as a News Source
Real-time public tweets provide instant news updates.
Users discuss politics, entertainment, business, and personal lives.
Mobile users post photos/videos of live events.
Terms like Twitterverse & Twittersphere refer to Twitter's global user base.
What is Twitter?
Founded in 2006 as a microblogging platform.
Tweets were initially 140 characters, now 280 for most languages.
Open-follow system, unlike Facebook/LinkedIn, where connections are
mutual.
Twitter Statistics
Millions of users & tweets per second.
Some users have 100M+ followers.
Active users tweet multiple times per day.
Live tweet streams are like “drinking from a fire hose” due to rapid updates.
Twitter & Big Data
A key data source for research & business analytics.
Free API provides limited access to recent tweets.
Paid API offers access to full historical data.
Challenges & Cautions
Tweets can contain false/misleading information (e.g., market
manipulation, election influence).
Hedge funds use tweet analysis but remain cautious.
Web service limitations:
Internet issues may disrupt access.
API changes can impact programs.
Regional restrictions may apply.
Write a Python code to fetch user information from Twitter's API using the
requests package and a Bearer Token.
import requests
# Replace with your actual Bearer Token from X Developer Portal
BEARER_TOKEN = "GIVE YOUR BEARER TOKEN"
def get_user_info(username):
url =
f"https://api.twitter.com/2/users/by/username/{username}?user.fields=id,name,username,description,public_metrics
"
headers = {
"Authorization": f"Bearer {BEARER_TOKEN}"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
user_data = response.json()["data"]
print("User ID:", user_data["id"])
print("User Name:", user_data["name"])
print("Screen Name:", user_data["username"])
print("Description:", user_data["description"])
print("Followers Count:", user_data["public_metrics"]["followers_count"])
else:
print("Error:", response.status_code, response.text)
# Replace 'elonmusk' with any Twitter username
get_user_info("GIVE YOUR TWITTER USERNAME")
Understanding the code
import requests
The requests library is used to make HTTP requests to
Twitter’s API.
Here, it will be used to send a GET request to
Twitter’s API.
BEARER_TOKEN = "YOUR BEARER TOKEN“
The Bearer Token is a unique key generated from the
Twitter Developer Portal.
It authenticates the request and grants permission to
access Twitter’s API.
def get_user_info(username):
Defines a function named get_user_info() that takes a
Twitter username as input.
url =
fhttps://api.twitter.com/2/users/by/username/{userna
me}?user.fields=id,name,username,description,public
_metrics
This URL endpoint requests user details from Twitter
API v2.
{username} is a placeholder that gets replaced by the
actual Twitter handle.
user.fields = id, name, username, description, public_metrics specifies which
details we want to fetch:
id → Unique ID of the user.
name → Display name of the user.
username → Twitter handle (@username).
description → Bio of the user.
public_metrics → Includes follower count.
headers = {
"Authorization": f"Bearer {BEARER_TOKEN}"
}
This adds an Authorization header with the Bearer Token to authenticate the
request.
response = requests.get(url, headers=headers)
Sends a GET request to Twitter’s API using the constructed url and header.
response stores the API response containing user data.
if response.status_code == 200:
Checks if the request was successful (HTTP status code 200 = OK).
If successful, it extracts and prints user details.
user_data = response.json()["data"]
Converts the API response to JSON format and retrieves the “data” dictionary.
response.json() converts the API response to a Python dictionary.
["data"] extracts the relevant user information.
response.status_code:
Retrieves the HTTP status code returned by the server.
200: OK(Success)
400: Bad Request (client error)
401: Unauthorized (invalid token)
403: Forbidden (no permission)
404: Not Found (invalid URL)
response.text:
Displays the response body (content) returned by the server.
It contains additional information about the error, often in JSON format.
Write a Python code to fetch user details, including follower count, from
Twitter's API using the tweepy package and authentication credentials.
import tweepy
# Twitter API credentials
API_KEY = " GIVE YOUR API KEY"
API_SECRET = " GIVE YOUR API SECRET"
ACCESS_TOKEN = " GIVE YOUR ACCESS TOKEN"
ACCESS_SECRET = "GIVE YOUR ACCESS SECRET"
BEARER_TOKEN = "GIVE YOUR BEARAER TOKEN"
# Authenticate with Twitter API v2
client = tweepy.Client(bearer_token=BEARER_TOKEN)
# Twitter username (replace with your username)
username = "GIVE YOUR TWITTER USERNAME"
# Fetch user details
user = client.get_user(username=username, user_fields=["public_metrics"])
# Get follower count
if user.data:
print("User ID:", user.data.id)
print("Username:", user.data.username)
print("Name:", user.data.name)
print("Followers Count:", user.data.public_metrics["followers_count"])
else:
print("User not found.")
Importing the tweepy library
Imports the Tweepy library, which is used to interact
with the Twitter API in Python.
It allows you to authenticate, send requests, and
retrieve data from Twitter.
These are Twitter API credentials required for authentication:
API_KEY: Your consumer API key from the X Developer Portal.
API_SECRET: The API secret key (linked to the API key).
ACCESS_TOKEN: Your access token used for making requests on behalf of
your account.
ACCESS_SECRET: Your access token secret, used along with the access token
to authenticate requests.
BEARER_TOKEN: Used to authenticate with Twitter API v2.
Authenticating with Twitter API v2
client = tweepy.Client(bearer_token=BEARER_TOKEN)
This authenticates your application with Twitter API v2 using the
BEARER_TOKEN.
The Bearer Token is specifically for read-only access and is used for
making GET requests (e.g., fetching tweets, user info).
It simplifies authentication without the need for API_KEY and
ACCESS_TOKEN.
tweepy.Client(): The client object provides access to Twitter’s v2
endpoints.
This allows you to:
Retrieve user data.
Get tweet metrics.
Interact with the Twitter API.
Fetching User Details
user = client.get_user(username=username,
user_fields=["public_metrics"])
client.get_user(): Sends a GET request to the Twitter API
to retrieve the user’s details.
Parameters:
username=username: Specifies the Twitter username
to search.
user_fields=["public_metrics"]: Includes public
metrics in the response, such as:
Followers count
Following count
Tweet count
Checking if the User Exists, Extracting and Displaying the user Info
if user.data: Checks if the API returned valid data for the
given username.
If the user exists, it proceeds to extract and display the
information.
Displays the user's information:
user.data.id: The unique Twitter User ID.
user.data.username: The Twitter handle (e.g., elonmusk).
user.data.name: The display name (e.g., Elon Musk).
user.data.public_metrics["followers_count"]: Retrieves the
follower count from the public_metrics field.
Write a Python code to search for recent tweets containing a specific keyword
using the tweepy package and Twitter API authentication.
import tweepy
API_KEY = " GIVE YOUR API KEY"
API_SECRET = " GIVE YOUR API SECRET"
ACCESS_TOKEN = " GIVE YOUR ACCESS TOKEN"
ACCESS_SECRET = "GIVE YOUR ACCESS SECRET"
client = tweepy.Client(bearer_token="GIVE YOUR BEARAER TOKEN")
query = "Python programming" # Replace with your search term
tweets = client.search_recent_tweets(query=query, max_results=15)
# Print tweets
for tweet in tweets.data:
print(f"Tweet: {tweet.text}\n")
Defining the Search Query, Fetching Recent Tweets
query = "Python programming“
Defines the search query to filter tweets.
In this case, the code is searching for recent tweets that contain the
phrase "Python programming“.
tweets = client.search_recent_tweets(query=query,
max_results=15)
client.search_recent_tweets(): A function that sends a GET request
to the Twitter API to fetch recent tweets.
Parameters:
query=query: The search term you want to look for.
max_results=15: The maximum number of tweets to retrieve.
Displaying the Tweets
for tweet in tweets.data:
print(f"Tweet: {tweet.text}\n")
for tweet in tweets.data: Iterates through the list of
retrieved tweets.
tweet.text: Displays the content of each tweet.
tweepy.OAuthHandler
Used to create an OAuthHandler object for
authentication.
Example:
import tweepy
auth = tweepy.OAuthHandler('API_KEY',
'API_SECRET')
OAuthHandler.set_access_token
Used to set the access token and secret.
Example:
auth.set_access_token('ACCESS_TOKEN',
'ACCESS_TOKEN_SECRET')
api=tweepy.API
Used to create an API object for interacting with
Twitter.
Example:
api = tweepy.API(auth)
api.get_user()
Used to get information about a Twitter account.
Example:
user = api.get_user(screen_name='TwitterUser')
print(user.name, user.followers_count)
api.followers
Used to get a list of followers for an account.
Example:
followers = api.followers(screen_name='TwitterUser')
for f in followers:
print(f.name)
api.followers_count
Returns the number of followers.
Example:
user = api.get_user(screen_name='TwitterUser')
print(user.followers_count)
api.followers_ids
Used to get a list of follower IDs for an account.
Example:
follower_ids =
api.followers_ids(screen_name='TwitterUser')
print(follower_ids)
api.friends
Used to get a list of friends (accounts followed) for an
account.
Example:
friends = api.friends(screen_name='TwitterUser')
for f in friends:
print(f.name)
api.friends_count
Used to get number of friends (accounts followed).
Example:
user = api.get_user(screen_name='TwitterUser')
print(user.friends_count)
api.me()
Returns a User object for the authenticated account.
Example:
me = api.me()
print(me.name, me.screen_name)
Tweepy.cursor
Handles pagination and rate limits when accessing
large datasets.
Example:
for tweet in tweepy.Cursor(api.user_timeline,
screen_name='TwitterUser').items(10):
print(tweet.text)
api.user_timeline
Used to get tweets from a user's timeline.
Example:
tweets =
api.user_timeline(screen_name='TwitterUser',
count=5)
for tweet in tweets:
print(tweet.text)
api.home_timeline
Used to get tweets from the home timeline.
Example:
tweets = api.home_timeline(count=5)
for tweet in tweets:
print(tweet.text)
api.search
Used to search for tweets matching a query.
Example:
tweets = api.search(q='Python', count=5)
for tweet in tweets:
print(tweet.text)
api.trends_available
Gets a list of locations with trending topics.
Example:
trends = api.trends_available()
print(trends)
api.trends_closest
Finds locations close to a specified latitude and
longitude.
Example:
closest_trends = api.trends_closest(lat=37.7749, long=-
122.4194)
print(closest_trends)
api.trends_place
Gets trending topics for a specific location.
Example:
trends = api.trends_place(1)
print(trends)
WordCloud
Used to create a word frequency cloud.
Example:
from wordcloud import WordCloud
wordcloud = WordCloud().generate('sample text')
wordcloud.to_image().show()
clean(text)
Cleans a tweet by removing URLs, mentions, hashtags,
emojis, reserved words, and numbers.
Example:
from preprocessor import clean
tweet = 'Check this out! http://example.com #AI
@user'
print(clean(tweet))
tokenize(text)
Tokenizes the tweet while keeping URLs, mentions,
and hashtags.
Example:
from preprocessor import tokenize
tweet = 'Hello #World! Visit http://example.com'
print(tokenize(tweet))
set_options(*options)
Specifies what to remove (e.g., mentions, URLs,
hashtags).
Example:
from preprocessor import set_options, clean
set_options('urls', 'hashtags')
tweet = 'Visit http://example.com #Python'
print(clean(tweet))
clear()
Clears the set options, resetting the preprocessor.
Example:
from preprocessor import clear
clear()
on_status
Called when a new tweet arrives.
Example:
class MyListener(tweepy.StreamListener):
def on_status(self, status):
print(status.text)
on_connect
Called when connection to the stream is successful.
Example:
def on_connect(self):
print('Connected to stream.')
on_limit
Called when tweet stream exceeds rate limits.
Example:
def on_limit(self, track):
print('Rate limit exceeded')
on_error
Called when Twitter sends an error code.
Example:
def on_error(self, status_code):
print(f'Error: {status_code}')
on_delete
Called when a tweet is deleted.
Example:
def on_delete(self, status_id, user_id):
print(f'Tweet {status_id} deleted')
on_data
Called when raw data is received from Twitter.
Example:
def on_data(self, raw_data):
print(raw_data)
on_timeout
Called if the connection to the stream times out.
Example:
def on_timeout(self):
print('Stream timeout')
on_warning
Called when Twitter sends a disconnect warning.
Example:
def on_warning(self, notice):
print('Warning:', notice)