簡體   English   中英

具有next_results的Twython搜索API

[英]Twython search API with next_results

我對搜索API有點困惑。 我們假設我查詢“foobar”,代碼如下:

from twython import Twython
api = Twython(...)
r = api.search(q="foobar")

通過這種方式,我在r["metadata"]有15個狀態和"next_results" 有沒有辦法將這些元數據退回到Twython API並且還具有以下狀態更新,或者我是否可以從"next_results"手動獲取下一個until_id並執行全新查詢?

petrux,“next_results”返回元數據“max_id”和since_id,它們應該用於反彈並循環播放時間線,直到我們得到所需數量的推文。

以下是Twitter上有關如何操作的更新: https//dev.twitter.com/docs/working-with-timelines

以下是可能有用的示例代碼。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM