我正在尝试从@realDonaldTrump获取所有5万条推文。我知道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]
答案 0 :(得分:0)
免费提供免费的开发者帐户,您不会获得比最近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