简体   繁体   中英

get tweets with specific status ids using tweepy

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

 API.get_status(id)

Returns a single status specified by the ID parameter.
Parameters: id – The numerical ID of the status.
Return type:    Status object

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.

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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