如何获取用户的所有推文?

时间:2013-02-12 21:30:04

标签: json twitter

我正在尝试获取所有特定用户的推文。

我知道有3600条推文的回访限制,所以我想知道为什么我不能从这一行获得更多推文:

https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=mybringback&count=3600

有谁知道如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

API文档指定此调用将返回的最大状态数为200。

https://dev.twitter.com/docs/api/1/get/statuses/user_timeline

  

指定要尝试和检索的推文数量,最多为200. count的值最好被视为要返回的推文数量的限制,因为暂停或删除的内容在计数后被删除应用。即使未提供include_rts,我们也会在计数中包含转推。建议您在使用此API方法时始终发送include_rts = 1。

答案 1 :(得分:0)

这是我用于一个必须做的项目的东西:

import json
import commands

import time

def get_followers(screen_name):

    followers_list = []

    # start cursor at -1
    next_cursor = -1

    print("Getting list of followers for user '%s' from Twitter API..." % screen_name)

    while next_cursor:

        cmd = 'twurl "/1.1/followers/ids.json?cursor=' + str(next_cursor) + \
                '&screen_name=' + screen_name + '"'

        (status, output) = commands.getstatusoutput(cmd)

        # convert json object to dictionary and ensure there are no errors
        try:
            data = json.loads(output)

            if data.get("errors"):

                # if we get an inactive account, write error message
                if data.get('errors')[0]['message'] in ("Sorry, that page does not exist",
                                                        "User has been suspended"):

                    print("Skipping account %s. It doesn't seem to exist" % screen_name)
                    break

                elif data.get('errors')[0]['message'] == "Rate limit exceeded":
                    print("\t*** Rate limit exceeded ... waiting 2 minutes ***")
                    time.sleep(120)
                    continue

                # otherwise, raise an exception with the error
                else:

                    raise Exception("The Twitter call returned errors: %s"
                                    % data.get('errors')[0]['message'])

            if data.get('ids'):
                print("\t\tFound %s followers for user '%s'" % (len(data['ids']), screen_name))
                followers_list += data['ids']

            if data.get('next_cursor'):
                next_cursor = data['next_cursor']
            else:
                break

        except ValueError:
            print("\t****No output - Retrying \t\t%s ****" % output)

    return followers_list



screen_name = 'AshwinBalamohan' 
followers = get_followers(screen_name)
print("\n\nThe followers for user '%s' are:\n%s" % followers)

为了实现这一点,您需要安装Ruby gem'Twurl',可在此处获取:https://github.com/marcel/twurl

我发现Twurl比其他Python Twitter包装器更容易使用,因此选择从Python调用它。如果您希望我指导您如何安装Twurl和Twitter API密钥,请告诉我。