[go: up one dir, main page]

0% found this document useful (0 votes)
7 views7 pages

Data Mining Twitter

The document provides Python code examples for interacting with Twitter's API using both the requests package and the tweepy package. It demonstrates how to fetch user information, including follower count, and search for recent tweets containing specific keywords. The examples include necessary authentication details and show how to handle API responses.

Uploaded by

Pavilion
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Data Mining Twitter

The document provides Python code examples for interacting with Twitter's API using both the requests package and the tweepy package. It demonstrates how to fetch user information, including follower count, and search for recent tweets containing specific keywords. The examples include necessary authentication details and show how to handle API responses.

Uploaded by

Pavilion
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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 =
"AAAAAAAAAAAAAAAAAAAAADPd0AEAAAAAHu0Unh8Sen%2Fq
gq7AuU%2FX9KKS82s%3DIRNnz2aPtwD6WkrLyOiW9tHssDeFt
goj9ujP6T2VU7an9YtT1N"

def get_user_info(username):
url =
f"https://api.twitter.com/2/users/by/username/{username}?us
er.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)

get_user_info("RoutrayPravat81")
Output:
User ID: 1905128428837462017
User Name: Pravat Kumar Routray
Screen Name: RoutrayPravat81
Description:
Followers Count: 0

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 = " 5p3jztPwodYV9oPIgOAFCdBq1"
API_SECRET = "
6fW2GxaQUaTP4obdl3iQ2XURYZUDSOWTfQWazaKXq21shKjA1
m"
ACCESS_TOKEN =
"1905128428837462017-jHthYXsIIPJbyRvMqlJNr8Q4a9UBWr"
ACCESS_SECRET =
"x3Asg0xm6pHWoCj16KIbpSHfjOxF29iKifVCXJEHtttBI"
BEARER_TOKEN =
"AAAAAAAAAAAAAAAAAAAAADPd0AEAAAAAPOTdTYfxxbQb%2F
z5HDuJKo7dMYuY%3DUgJgWH4ELRLm0xorRmAbikDxo5xEBJ0lf
YLqA1X3Y1FZHH69tj"
# Authenticate with Twitter API v2
client = tweepy.Client(bearer_token=BEARER_TOKEN)
# Twitter username (replace with your username)
username = "RoutrayPravat81"
# 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.")
Output:
User ID: 1905128428837462017
Username: RoutrayPravat81
Name: Pravat Kumar Routray
Followers Count: 0
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 = " 5p3jztPwodYV9oPIgOAFCdBq1"
API_SECRET =
"6fW2GxaQUaTP4obdl3iQ2XURYZUDSOWTfQWazaKXq21shKjA1
m"
ACCESS_TOKEN =
"1905128428837462017-jHthYXsIIPJbyRvMqlJNr8Q4a9UBWr"
ACCESS_SECRET =
"x3Asg0xm6pHWoCj16KIbpSHfjOxF29iKifVCXJEHtttBI"
client =
tweepy.Client(bearer_token="AAAAAAAAAAAAAAAAAAAAADPd0
AEAAAAAPOTdTYfxxbQb%2Fz5HDuJKo7dMYuY%3DUgJgWH4EL
RLm0xorRmAbikDxo5xEBJ0lfYLqA1X3Y1FZHH69tj")
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")
Output:
Tweet: RT @EricOkemwa_: If you’re aiming for
something like systems programming, embedded tech,
or even just a deeper grasp of how higher-level la…

Tweet: RT @Python_Dv: Python Question / Quiz;

🤔🚀 👇
What is the output of the following Python code, and
why? Comment your answers below!

#python…

Tweet: RT @swapnakpanda: "Competitive Programming in


Python"

This 267-pages book from Cambridge University will


teach you 128 Algorithms. Don't m…

Tweet: RT @Python_Dv: Python Question / Quiz;


🤔🚀 👇
What is the output of the following Python code, and
why? Comment your answers below!

#python…

Tweet: RT @cresbabiera: Salute to BSV Ambassador Ken


Sato (@Uncle_Nakamoto) and his incredible team at
YenPoint Inc! On March 26, just 5 days afte…

Tweet: drop out ufanye bootcamp basi

if you don't want to learn the basics of OOP through


.net using visual basic or c# then you have no
business learning other emerging technologies and
languages

shida yenu mnafikiria tech degree ni kufanya python


programming

it's doing everything https://t.co/QTGDLLbgDS

Tweet: RT @swapnakpanda: "Competitive Programming in


Python"

This 267-pages book from Cambridge University will


teach you 128 Algorithms. Don't m…

Tweet: Review of the best online courses in the


market about statistics and probability, machine
learning, and python programming

https://t.co/wn4AaIRck2

Tweet: Review of the best online courses in the


market about statistics and probability, machine
learning, and python programming

https://t.co/wn4AaIRck2
Tweet: Review of the best online courses in the
market about statistics and probability, machine
learning, and python programming

https://t.co/wn4AaIRck2

Tweet: Review of the best online courses in the


market about statistics and probability, machine
learning, and python programming

https://t.co/wn4AaIRck2

Tweet: Review of the best online courses in the


market about statistics and probability, machine
learning, and python programming

https://t.co/wn4AaIRck2

Tweet: Great books about Python Programming, Machine


Learning, and AI
https://t.co/18xVM0F4Oo

Tweet: Great books about Python Programming, Machine


Learning, and AI
https://t.co/18xVM0F4Oo

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)

You might also like