gpt4 book ai didi

Issue with Accessing Twitter API using Tweepy with Free Level Access(使用具有自由级访问权限的Tweepy访问Twitter API时出现问题)

转载 作者:bug小助手 更新时间:2023-10-28 22:26:33 25 4
gpt4 key购买 nike



I've implemented a Python script using tweepy to fetch the most recent tweets of a user and save them to an Excel file. Here's a simplified version of my code:

我已经实现了一个使用tweetkey的Python脚本来获取用户最近的tweet并将其保存到Excel文件中。下面是我的代码的简化版本:


import tweepy
import pandas as pd

# My Twitter API credentials
API_KEY = 'x'
API_SECRET_KEY = 'x'
ACCESS_TOKEN = 'xx'
ACCESS_TOKEN_SECRET = 'xxxx'

# Authentication with tweepy
auth = tweepy.OAuthHandler(API_KEY, API_SECRET_KEY)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)

def get_tweets(username, count=200):
tweets = api.user_timeline(screen_name=username, count=count, tweet_mode="extended")
tweet_data = [{
'tweet_id': tweet.id,
'created_at': tweet.created_at,
'text': tweet.full_text,
'likes': tweet.favorite_count,
'retweets': tweet.retweet_count
} for tweet in tweets]
return tweet_data

def main():
user = 'x'
data = get_tweets(user, count=200)

df = pd.DataFrame(data)
df.to_excel('twitter_data.xlsx', index=False)

if __name__ == "__main__":
main()

However, I encounter the following error when running the script:

但是,我在运行该脚本时遇到以下错误:


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

It's worth noting that I'm currently on the Free Level Access of the Twitter API.

值得注意的是,我目前正在访问Twitter API的免费级别。


Do I need to upgrade my access to use this endpoint or is there another solution to access the data using the Free Level Access?

我是否需要升级我的访问权限才能使用此终结点,或者是否有其他解决方案可以使用自由级访问权限访问数据?


更多回答
优秀答案推荐

It looks like you are using the Twitter API v1.1, which no longer supports the Search Recent Tweets feature.

看起来你正在使用Twitter API v1.1,它不再支持搜索最近的推文功能。


You have to utilize the Twitter API v2 to use the same feature, but it requires Basic Level Access.

您必须使用Twitter API v2才能使用相同的功能,但它需要基本级别的访问权限。


Example:

例如:


# 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"
max_tweets = 100
searched_tweets = tweepy.Paginator(client.search_recent_tweets, query=query, max_results=max_tweets).flatten(limit=max_tweets)

If you are planning to retrieve a maximum of 100 tweets at a time, then you don't need Pagination.

如果您计划一次最多检索100条tweet,那么您不需要分页。


So, you can use the following line of code:

因此,您可以使用以下代码行:


searched_tweets = client.search_recent_tweets(query=query, max_results=max_tweets)

For more information:

有关详细信息,请参阅:


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

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


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

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


https://docs.tweepy.org/en/latest/v2_pagination.html

Https://docs.tweepy.org/en/latest/v2_pagination.html


I hope this helps.

希望这能帮上忙。


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com