dynamic
dynamic

Reputation: 1

ERROR 453 Tweepy crawling question using Twitter API V2 (Basic Level)

I am a student who just studied Python. I want to crawl using Twitter API v2. The error 453 keeps showing that the v2 endpoint cannot be reached. I've also upgraded to Basic, but I can't find out what's wrong with the code, even if I look up various sites and code examples.

import tweepy
import pandas as pd

api_key = "Your API Key"
api_secret = "Your API Secret"
access_token = "Your Access Token"
access_token_secret = "Your Access Token Secret"

auth = tweepy.OAuthHandler(api_key, api_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

username = "target_username" 
query = f"from:{username}"    
start_date = "2023-01-01"    
end_date = "2023-01-31"       

tweets = []
try:
    for tweet in tweepy.Cursor(api.search, q=query, tweet_mode="extended", since=start_date, until=end_date).items():
        tweet_info = {
            "Tweet": tweet.full_text,
            "Likes": tweet.favorite_count,
            "Retweets": tweet.retweet_count,
            "Replies": tweet.reply_count
        }
        tweets.append(tweet_info)
except tweepy.TweepError as e:
    print("Error: " + str(e))

df = pd.DataFrame(tweets)

output_file = "tweets_with_metrics.xlsx"
df.to_excel(output_file, index=False)

print("message")

453 - You currently have access to a subset of Twitter API v2 endpoints and limited v1.1 endpoints (e.g. media post, oauth) only. If you need access to this endpoint, you may need a different access level. You can learn more here: https://developer.twitter.com/en/portal/product

I want to crawl the number of tweets, likes, comments, and retweets for a specific period of time on a specific account.

Upvotes: 0

Views: 434

Answers (1)

Futurist Forever
Futurist Forever

Reputation: 516


It seems like you are following outdated documentation for the Twitter API.

The correct syntax to call the Twitter API v2 is:

# Authenticate to Twitter
client = tweepy.Client(
    consumer_key=CONSUMER_KEY,
    consumer_secret=CONSUMER_SECRET,
    access_token=ACCESS_TOKEN,
    access_token_secret=ACCESS_TOKEN_SECRET
)

# Search Tweets
query = "Twitter"
tweets = client.search_recent_tweets(query=query, max_results=10)

For more information:

https://developer.twitter.com/en/docs/twitter-api

https://docs.tweepy.org/en/stable/client.html#tweepy.Client.search_recent_tweets

I hope this helps.

Upvotes: 0

Related Questions