我正在努力将我的网站从 Bing Azure API (v2) 迁移到新的 Bing V5 搜索 API。
在旧的 API 上,对象使用这个“__next”来判断他后面是否还有其他东西。
但在新的 API 上,json 不再返回此值。
我正在努力升级我的分页,但我不知道如果没有这个元素该怎么做。
有谁知道新 API 中的替换内容是什么?
我在他们的迁移指南或新的 V5 API 指南中找不到任何信息。
谢谢。
最佳答案
约翰是对的。您可以将 count
和 offset
参数与返回的第一个对象的 json 中的值的 totalEstimatedMatches
结合使用。
示例:想象一下,您非常喜欢橡皮鸭,以至于您希望存在的每个网页都包含术语“橡皮鸭”。好运不列颠哥伦比亚省,这不是互联网的运作方式。不过,先别自杀,Bing 对包含“橡皮鸭”的网页了解很多,您所需要做的就是对 Bing 了解并高兴的与“橡皮鸭”相关的网站进行分页。
首先,我们需要通过向 API 传递“rubber-ducky”来告诉 API 我们想要“一些”结果(“一些”的值由
count
参数定义) ,最大为 50)。接下来,我们需要查看返回的第一个 JSON 对象;这将告诉我们 Bing 在名为
totalEstimatedMatches
的字段中了解多少个“橡皮鸭”网站。由于我们对与橡皮鸭相关的网站有着无法满足的渴望,因此我们将设置一个 while 循环来交替黑白查询和递增
offset
,这确实直到totalEstimatedMatches
和 offset 相距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)
希望这能有所帮助。
关于azure - bing 搜索 api v5 "__next"替换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40628260/