简体   繁体   English

使用 tweepy 获取特定用户的所有推文

[英]Getting all tweets from certain user with tweepy

I am trying to get all 50k tweets from @realDonaldTrump.我正在尝试从@realDonaldTrump 获取所有 50k 条推文。 I know there is limit for twitter api requests, so I am using max_id=oldest.我知道 twitter api 请求有限制,所以我使用 max_id=oldest。 But I only get 995 tweets.但我只收到 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]

Free the free dev account you wont get more than the last 3200 tweets.释放免费的开发者帐户,您不会获得超过最后 3200 条推文。

I suggest to use cursor and pages.我建议使用 cursor 和页面。

..
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM