【问题标题】:Python. TweepError: [{'code': 34, 'message': 'Sorry, that page does not exist.'}]Python。 TweepError: [{\'code\': 34, \'message\': \'抱歉,该页面不存在。\'}]
【发布时间】:2023-02-20 21:42:53
【问题描述】:

我想运行这段代码

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 用户信息=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': '抱歉,该页面不存在。'}]

如何解决这个问题?

【问题讨论】:

    标签: python python-3.x api twitter tweepy


    【解决方案1】:

    使用 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))
    

    结果

    Bearer Token 通过 Token API 获取或从您的 Developer Dashboard 复制

    https://api.twitter.com/oauth2/token?grant_type=client_credentials
    

    https://developer.twitter.com/
    

    【讨论】:

      猜你喜欢
      • 2012-10-06
      • 2015-06-24
      • 2017-12-12
      • 2015-04-22
      • 2016-02-26
      • 2014-03-10
      • 2021-07-04
      • 2012-10-13
      • 1970-01-01
      相关资源
      最近更新 更多