【问题标题】:How do I get all tweets of a user?如何获取用户的所有推文?
【发布时间】:2013-01-28 07:52:47
【问题描述】:

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

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

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

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

【问题讨论】:

  • 在 Python / Ruby 中可以这样做吗?

标签: json twitter


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    这是我在一个必须这样做的项目中使用的东西:

    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 密钥,请告诉我。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 2017-07-14
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    相关资源
    最近更新 更多