2016-11-16 111 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) 

希望这有助于一点。