我想运行这段代码
def get_all_tweets(屏幕名称):
#initialize a list to hold all the tweepy Tweets
alltweets = []
#make initial request for most recent tweets (200 is the maximum allowed count)
new_tweets = api.user_timeline(screen_name = screen_name,count=200)
#save most recent tweets
alltweets.extend(new_tweets)
#save the id of the oldest tweet less one
oldest = alltweets[-1].id - 1
while len(new_tweets) > 0:
new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest)
#save most recent tweets
alltweets.extend(new_tweets)
# print(alltweets[-1]['id'])
#update the id of the oldest tweet less one
oldest = alltweets[-1].id - 1
TweepError Traceback(最后一次调用) 在 用户名中的 x 为 221: 第222章 --> 223 user_info=get_all_tweets(x)
224 all_fet.append(compute_features(x,user_info))
225 #
2 帧 /usr/local/lib/python3.8/dist-packages/tweepy/binder.py 在 execute(self) 第232回 233 其他: --> 234 raise TweepError(error_msg, resp, api_code=api_error_code) 235 236 # 解析响应负载
TweepError: [{'code': 34, 'message': 'Sorry, that page does not exist.'}]
如何解决这个问题?
使用
Tweepy
的V2代替V1.
它使更多的细节选择和灵活。
使用
get_users_tweets()
可以获取用户的推文。
import tweepy
def get_all_tweets(user_name):
bearer_token ="<your access token>"
client = tweepy.Client(bearer_token=bearer_token)
user = client.get_user(username=user_name)
user_id = user.data.id
count = 0
next_token = 1
limit = 1000
userTweets = []
while next_token != None and count < limit:
tweets = client.get_users_tweets(id=user_id, max_results=100)
if tweets.data is not None:
for tweet in tweets.data:
count += 1
userTweets.append({
"id": tweet.id,
"test": tweet.text
})
if (tweets.meta['next_token']) is not None:
next_token = tweets.meta['next_token']
else:
next_token = None
else:
next_token = None
return userTweets
userTweets = get_all_tweets('elonmusk')
for tweet in userTweets:
print(tweet)
print(len(userTweets))
Berer 令牌通过令牌 API 获取或从您的开发人员仪表板复制
https://api.twitter.com/oauth2/token?grant_type=client_credentials
https://developer.twitter.com/