【问题标题】:How do I return more than 100 Twitter search results with Twython?如何使用 Twython 返回超过 100 个 Twitter 搜索结果?
【发布时间】:2014-01-29 09:19:05
【问题描述】:

Twitter 在 API 上返回搜索结果时,每个“页面”仅返回 100 条推文。他们在返回的search_metadata 中提供了max_idsince_id,可用作获取较早/较晚推文的参数。

Twython 3.1.2 文档表明这种模式是搜索的“旧方式”:

results = twitter.search(q="xbox",count=423,max_id=421482533256044543)
for tweet in results['statuses']:
    ... do something

这是“new way”:

results = twitter.cursor(t.search,q='xbox',count=375)
for tweet in results:
    ... do something

当我执行后者时,它似乎会无休止地迭代相同的搜索结果。我正在尝试将它们推送到 CSV 文件,但它会推送大量重复项。

使用 Twython 搜索大量推文并遍历一组独特结果的正确方法是什么?

编辑:这里的另一个问题是,当我尝试使用生成器 (for tweet in results:) 进行迭代时,它会反复循环,不会停止。啊——这是个bug……https://github.com/ryanmcgrath/twython/issues/300

【问题讨论】:

    标签: python twitter twython


    【解决方案1】:

    我遇到了同样的问题,但您似乎应该使用 max_id 参数分批遍历用户的时间线。根据 Terence 的回答,批次应该是 100(但实际上,对于 user_timeline,200 是最大计数),并且只需将 max_id 设置为上一组返回的推文中的最后一个 id 减一(因为 max_id 包含在内)。代码如下:

    '''
    Get all tweets from a given user.
    Batch size of 200 is the max for user_timeline.
    '''
    from twython import Twython, TwythonError
    tweets = []
    # Requires Authentication as of Twitter API v1.1
    twitter = Twython(PUT YOUR TWITTER KEYS HERE!)
    try:
        user_timeline = twitter.get_user_timeline(screen_name='eugenebann',count=200)
    except TwythonError as e:
        print e
    print len(user_timeline)
    for tweet in user_timeline:
        # Add whatever you want from the tweet, here we just add the text
        tweets.append(tweet['text'])
    # Count could be less than 200, see:
    # https://dev.twitter.com/discussions/7513
    while len(user_timeline) != 0: 
        try:
            user_timeline = twitter.get_user_timeline(screen_name='eugenebann',count=200,max_id=user_timeline[len(user_timeline)-1]['id']-1)
        except TwythonError as e:
            print e
        print len(user_timeline)
        for tweet in user_timeline:
            # Add whatever you want from the tweet, here we just add the text
            tweets.append(tweet['text'])
    # Number of tweets the user has made
    print len(tweets)
    

    【讨论】:

    • 其中一个问题是文档中推荐的新 cursor 方法会无限循环搜索结果,并且从不使用 max_id 进行搜索。
    • @Clay,差不多四年后,这似乎仍然是正确的。嘎。
    【解决方案2】:

    根据official Twitter API documentation

    计数可选

    每页返回的推文数量,最多 100 条

    【讨论】:

    • 我知道这一点。例如,我应该如何使用 Twython 返回 425 个搜索结果?我的理解是,Twython 中的 .cursor 格式会使用 max_id 遍历搜索结果页面以避免重复,但这似乎不会发生。
    【解决方案3】:

    您需要重复调​​用 python 方法。但是,不能保证这些将是下一个 N,或者如果推文真的出现,它可能会错过一些。

    如果您想在某个时间范围内查看所有推文,您可以使用流式 API:https://dev.twitter.com/docs/streaming-apis 并将其与 oauth2 模块结合使用。

    How can I consume tweets from Twitter's streaming api and store them in mongodb

    python-twitter streaming api support/example

    免责声明:我实际上没有尝试过这个

    【讨论】:

    • 其实根据 Twitter 的working with timelines 文档,你可以通过搜索来控制接收到的内容。当然,搜索的优势在于,您可以立即为低容量搜索词返回大量推文(通过流收集可能需要几个小时)。我只是好奇如何使用 Twython 来做到这一点。
    【解决方案4】:

    作为使用 Twython 为搜索查询返回 100 条推文的问题的解决方案,这里是显示如何使用“旧方式”完成此操作的链接:

    Twython search API with next_results

    【讨论】:

      猜你喜欢
      • 2012-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-02
      • 2014-02-25
      相关资源
      最近更新 更多