【问题标题】:Getting all tweets from certain user with tweepy使用 tweepy 获取特定用户的所有推文
【发布时间】:2020-08-07 01:28:13
【问题描述】:

我正在尝试从@realDonaldTrump 获取所有 50k 条推文。我知道 twitter api 请求有限制,所以我使用 max_id=oldest。但我只收到 995 条推文。

import tweepy as tweepy

consumerKey = "xxx"
consumerSecret = "xxx"
accessToken = "xxx"
accessTokenSecret = "xxx"

auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
auth.set_access_token(accessToken, accessTokenSecret)

api = tweepy.API(auth, wait_on_rate_limit=True)
alltweets = []

username="@realDonaldTrump"

new_tweets = api.user_timeline(username, tweet_mode = 'extended', count=200)
alltweets.extend(new_tweets)
oldest = alltweets[-1].id - 1

while len(new_tweets) > 0:
    print(f"getting tweets before {oldest}")

    new_tweets = api.user_timeline(username, max_id=oldest,tweet_mode = 'extended', count=200)
    alltweets.extend(new_tweets)
    oldest = alltweets[-1].id - 1

    print(f"...{len(alltweets)} tweets downloaded so far")

outtweets = [[tweet.id_str, tweet.created_at, tweet.full_text] for tweet in alltweets]

【问题讨论】:

    标签: python twitter tweepy


    【解决方案1】:

    释放免费的开发者帐户,您不会获得超过最后 3200 条推文。

    我建议使用光标和页面。

    ..
    c = tw.Cursor(api.user_timeline, id=userid, tweet_mode="extended", wait_on_rate_limit=True,count=200).pages()
    while True:
        try:
            page = c.next()
            tweets.extend(page)
    ..
        except tw.TweepError:
            print(e)
            time.sleep(60)
            continue
        except StopIteration:
            break
    

    【讨论】:

    猜你喜欢
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 2016-10-20
    • 2020-03-12
    • 2021-02-25
    • 2017-02-11
    相关资源
    最近更新 更多