简体   繁体   中英

Twitter API - not collecting all tweets using Tweepy

I'm using Tweepy to collect tweets from the Twitter API by their Tweet ID.
Im trying to read in a file full of the IDs, get the previous tweet from the conversation stream, then store that tweet and its author's screen name etc in a text file. Some of the tweets have been deleted or the user's profile has been set to private, in which case I want to ignore that tweet and move on to the next. However, for some reason, I'm not collecting all accessible tweets. Its storing maybe 3/4 of all tweets that aren't private and haven't been deleted. Any ideas why its not catching everything?

Thanks in advance.

def getTweet(tweetID, tweetObj, callTweetObj, i):
    tweet = callTweetObj.text.encode("utf8")
    callUserName = callTweetObj.user.screen_name
    callTweetID = tweetObj.in_reply_to_status_id_str

    with open("call_tweets.txt", "a") as calltweets:
        output = (callTweetObj.text.encode('utf-8')+ "\t" + callTweetID + "\t" + tweetID)
        calltweets.write(output)
        print output 

    with open("callauthors.txt", "a") as callauthors:
        cauthors = (callUserName+ "\t" + "\t" + callTweetID + "\n")
        callauthors.write(cauthors)

    with open("callIDs.txt", "a") as callIDs:
        callIDs.write(callTweetID + "\n")

    with open("newResponseIDs.txt", "a") as responseIDs:
        responseIDs.write(tweetID)      

count = 0

file = "Response_IDs.txt"
with open(file, 'r+') as f:
    lines = f.readlines()
    for i in range(0, len(lines)):
        tweetID = lines[i]
        sleep(5)
        try:
            tweetObj = api.get_status(tweetID)
            callTweetID = tweetObj.in_reply_to_status_id_str
            callTweetObj = api.get_status(callTweetID)
            getTweet(tweetID, tweetObj, callTweetObj, i)
            count = count+1
            print count
        except:
            pass

You haven't specified information regarding the response coming back from api.get_status , so it's hard to detect what the error is.

However, it might be you have reached the rate limit for the statuses/show/:id request. The API specifies this request is limited to 180 requests a window.

You can use Tweepy to call application/rate_limit_status :

response = api.rate_limit_status()
remaining = response['resources']['statuses']['/statuses/show/:id']['remaining']
assert remaining > 0

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