2017-06-16 52 views
0

我有一个包含推特ID列表的文件,我想要检索这些推文。该文件包含超过10万个微博和Twitter的API允许仅检索100在tweepy中使用tweet ID检索推文列表

api = tweepy.API(auth) 
good_tweet_ids = [i for i in por.TweetID[0:100]] 
tweets = api.statuses_lookup(good_tweet_ids) 
for tweet in tweets: 
    print(tweet.text) 

有没有一种方法来获取更多的鸣叫说,1000或2000年,我不想取数据的样本,并将结果保存到一个文件并每次更改推特ID的索引,那么有没有办法做到这一点!?

回答

3

是 - twitter只允许您一次查找100条推文,但您可以在此之后立即查找另外100条推文。唯一值得关注的是速率限制 - 您受限于您可以在每15分钟窗口中对API进行的调用次数。幸运的是,当您使用wait_on_rate_limit=True创建API时,tweepy能够优雅地处理此问题。我们所需要做的就是将推特ID的完整列表分成100个或更少的批次(假设您有130个 - 第二个批次只应该是最终的30个),然后每次只查看一个。请尝试以下操作:

import tweepy 


def lookup_tweets(tweet_IDs, api): 
    full_tweets = [] 
    tweet_count = len(tweet_IDs) 
    try: 
     for i in range((tweet_count/100) + 1): 
      # Catch the last group if it is less than 100 tweets 
      end_loc = min((i + 1) * 100, tweet_count) 
      full_tweets.extend(
       api.statuses_lookup(id=tweet_IDs[i * 100:end_loc]) 
      ) 
     return full_tweets 
    except tweepy.TweepError: 
     print 'Something went wrong, quitting...' 

consumer_key = 'XXX' 
consumer_secret = 'XXX' 
access_token = 'XXX' 
access_token_secret = 'XXX' 

auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_token, access_token_secret) 

api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True) 

# do whatever it is to get por.TweetID - the list of all IDs to look up 

results = lookup_tweets(por.TweetID, api) 

for tweet in results: 
    if tweet: 
     print tweet.text 
+0

谢谢。它是一块蛋糕:) –

+0

不用担心朋友。如果它解决了您的问题,您可以使用它左侧的勾号将其标记为答案 – asongtoruin