Twitter API - 不使用Tweepy收集所有推文

时间:2016-03-14 12:27:39

标签: python twitter tweepy

我正在使用Tweepy通过推特ID从Twitter API收集推文 我试图读取一个充满ID的文件,从对话流中获取之前的推文,然后将该推文及其作者的屏幕名称等存储在文本文件中。一些推文已被删除或用户的个人资料已设置为私人,在这种情况下,我想忽略该推文并转到下一个。但是,出于某种原因,我没有收集所有可访问的推文。它存储的所有推文中有3/4是非私有且尚未删除的。任何想法为什么它没有抓住一切?

提前致谢。

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

1 个答案:

答案 0 :(得分:0)

您尚未指定有关从api.get_status返回的回复的信息,因此很难发现错误是什么。

但是,您可能已达到statuses/show/:id请求的费率限制。 API指定此请求仅限于180个窗口请求。

您可以使用Tweepy来致电application/rate_limit_status

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