首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于next_results的Twython

基于next_results的Twython
EN

Stack Overflow用户
提问于 2013-10-11 14:06:09
回答 1查看 6.4K关注 0票数 2

我对搜索API有点困惑。假设我查询"foobar",下面的代码如下:

代码语言:javascript
复制
from twython import Twython
api = Twython(...)
r = api.search(q="foobar")

这样,我就拥有了15个状态和一个"next_results" ( r["metadata"] )。是否有任何方法将这些元数据返回到Twython并进行以下状态更新,或者我是否可以从until_id手工获得下一个"next_results"并执行全新的查询?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-08 09:33:54

petrux,"next_results“是与元数据"max_id”和since_id一起返回的,这些元数据应该被用来弹回并遍历时间线,直到我们得到需要数量的tweet。

下面是twitter上关于如何做这件事的更新:https://dev.twitter.com/docs/working-with-timelines

下面是示例代码,可能会有所帮助。

代码语言:javascript
复制
tweets = []
MAX_ATTEMPTS = 10
COUNT_OF_TWEETS_TO_BE_FETCHED = 500 

for i in range(0,MAX_ATTEMPTS):

    if(COUNT_OF_TWEETS_TO_BE_FETCHED < len(tweets)):
        break # we got 500 tweets... !!

    #----------------------------------------------------------------#
    # STEP 1: Query Twitter
    # STEP 2: Save the returned tweets
    # STEP 3: Get the next max_id
    #----------------------------------------------------------------#

    # STEP 1: Query Twitter
    if(0 == i):
        # Query twitter for data. 
        results = api.search(q="foobar",count='100')
    else:
        # After the first call we should have max_id from result of previous call. Pass it in query.
        results = api.search(q="foobar",include_entities='true',max_id=next_max_id)

    # STEP 2: Save the returned tweets
    for result in results['statuses']:
        tweet_text = result['text']
        tweets.append(tweet_text)


    # STEP 3: Get the next max_id
    try:
        # Parse the data returned to get max_id to be passed in consequent call.
        next_results_url_params = results['search_metadata']['next_results']
        next_max_id = next_results_url_params.split('max_id=')[1].split('&')[0]
    except:
        # No more next pages
        break
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19320197

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档