2016-11-16 42 views
2

我正在將我的webstite表單Bing Azure API(v2)遷移到新的Bing V5搜索API。
在舊的API上,一個對象使用這個「__next」來告訴他是否有其他東西。
但是在新的API上,json不再返回這個。
我正在升級我的分頁,我不知道如何做到這一點沒有這個元素。
任何人都知道什麼在新的API中取代它?
我無法在其遷移指南或新的V5 API指南中找到任何信息。
謝謝。bing search api v5「__next」替換?

回答

0

約翰是對的。您可以使用countoffset params結合totalEstimatedMatches從返回的第一個對象的json中的值。

例子:想象一下,你愛橡膠duckies這麼多,你要在包含術語所有腦幹的每一個網頁的「橡膠鴨子」。這是不是互聯網如何運作。但是,不要自殺,但Bing知道很多關於含有「橡皮鴨」的網頁,你需要做的只是通過Bing知道並歡喜的'橡皮鴨'相關網站分頁。

  • 首先,我們需要通過傳遞「橡膠鴨子」,以它來告訴API,我們希望「一些」的結果(的「一些」是由count PARAM定義的值,50爲最大值) 。

  • 接下來,我們需要查看返回的第一個JSON對象;這將告訴我們在一個名爲totalEstimatedMatches的字段中Bing知道了多少個「橡皮鴨」的網站。

  • 由於我們對橡膠鴨子相關網站的渴求永無止境,我們要建立一個while循環是交替的B/W查詢和遞增offset並不會停止,直到totalEstimatedMatches和偏移量是count距離相隔。

下面是一些Python代碼澄清:

>>> import SomeMagicalSearcheInterfaceThatOnlyNeeds3Params as Searcher 
>>> 
>>> SearcherInstance = Searcher() 
>>> SearcherInstance.q = 'rubber-ducky' 
>>> SearcherInstance.count = 50 
>>> SearcherInstance.offset = 0 
>>> SearcherInstance.totalEstimatedMatches = 0 
>>> 
>>> print SearcherInstance.preview_URL 
'https://api.cognitive.microsoft.com/bing/v5.0/images/search?q=rubber%2Dducky&count=50&offset=0' 
>>> 
>>> json_return_object = SearcherInstance.search_2_json() 
>>> 
>>> ## Python just treats JSON as nested dictionaries. 
>>> tem = json_return_object['webPages']['totalEstimatedMatches'] 
>>> print tem 
9500000 
>>> num_links_returned = len(json_return_object['webPages']['value']) 
>>> print num_links_returned 
50 
>>> 
>>> ## We'll set some vals manually then make our while loop. 
>>> SearcherInstance.offset += num_links_returned 
>>> SearcherInstance.totalEstimatedMatches = tem 
>>> 
>>> a_dumb_way_to_store_this_much_data = [] 
>>>  
>>> while SearcherInstance.offset < SearcherInstance.totalEstimatedMatches: 
>>>  json_response = SearcherInstance.search_2_json() 
>>>  a_dumb_way_to_store_this_much_data.append(json_response) 
>>>  
>>>  actual_count = len(json_return_object['webPages']['value']) 
>>>  SearcherInstance.offset += min(SearcherInstance.count, actual_count) 

希望這有助於一點。