get tweets with specific status ids using tweepy

匿名 (未验证) 提交于 2019-12-03 01:00:01

问题:

I have a list of the specific status ids of tweets that I need to obtain. The tweepy documentation provides the following:

I can't work out how to use this or find any examples. Is this even the right thing?

My list of ids is 2240 items long and looks something like this:

response_ids = [717289507981107201, 717289501337509888, ..., 716684885411237888]

These ids were obtained from the 'in_response_to_status_id' field of tweets that I already have (I want to match the tweets I have to the tweets that they were written in response to).

I basically want to write something like

for id in response_ids:     tweet = API.get_status(id)

Any help on how to do this, or advice about whether this is possible, much appreciated.

回答1:

It is better to use the 'statuses_lookup' command. More infor in the below link http://docs.tweepy.org/en/v3.5.0/api.html#API.statuses_lookup

Before running the below program, get the consumer key and tokens.

import tweepy consumer_key = xxxx consumer_secret = xxxx access_token = xxxx access_token_secret = xxxx  auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret)  api = tweepy.API(auth)  tweets = api.statuses_lookup(id_list) # id_list is the list of tweet ids tweet_txt = [] for i in tweets:     tweet_txt.append(i.text)


回答2:

Think I've worked it out.

get_status does seem to be the right thing to use, although I initially had some problems with pagination errors. I've hacked some code found in response to another similar problem to come up with this solution:

def paginate(iterable, page_size):     while True:         i1, i2 = itertools.tee(iterable)         iterable, page = (itertools.islice(i1, page_size, None),                 list(itertools.islice(i2, page_size)))         if len(page) == 0:             break         yield page  index = 0 for page in paginate(response_ids, 1):     result = api.get_status(response_ids[index])._json     index += 1


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!