无法对所有 bing API 结果进行分页

Unable to paginate through all bing API results

我目前正在使用 Bing Web Search API v7 查询 Bing 的搜索结果。根据 API 文档,参数 countoffset 用于对结果进行分页,其总数在结果本身中由 [=14= 的值定义].

如下来自文档:

totalEstimatedMatches: The estimated number of webpages that are relevant to the query. Use this number along with the count and offset query parameters to page the results.

这似乎在一定程度上起作用,之后 API 只是继续 return 一遍又一遍地得到完全相同的结果,而不管 countoffset

在我的具体案例中,totalEstimatedMatches 设置为 330,000。对于 50count(即每个请求 50 个结果),结果在 offset 700 左右开始重复,即 3,500 结果进入估计的 330,000

在使用 bing 前端时,我注意到一旦页数足够高时就会出现类似的行为,例如

我是否错误地使用了 API,或者这只是某种限制或错误,其中 totalEstimatedMatches 刚刚好?

totalEstimatedMatches 提供网络上该查询的匹配总数 - 其中包括重复结果和近似相似的内容。

为了优化索引,所有搜索引擎都将结果限制为前 N 个网页。这就是你所看到的。这种行为在所有搜索引擎中都是一致的,因为通常几乎所有用户都会在 2-3 个搜索页面中更改 query/select 和 webpage/abandon。

简而言之,这不是 bug/incorrect 实施,而是限制您获得更多结果的索引优化。如果您确实需要获得更多结果,可以使用相关搜索并附加独特的网页。

从技术上讲,这并不是对问题的直接回答。希望它有助于提供一种通过 Bing 的 API 有效分页的方法,而不必使用 "totalEstimatedMatches" return 值,正如其他答案所解释的那样,它可以真正表现出来出乎意料: 这是一些 python:

class ApiWorker(object):
    def __init__(self, q):
        self.q = q
        self.offset = 0
        self.result_hashes = set()
        self.finished = False

    def calc_next_offset(self, resp_urls):
       before_adding = len(self.result_hashes)
       self.result_hashes.update((hash(i) for i in resp_urls)) #<==abuse of set operations.
       after_adding = len(self.result_hashes)
       if after_adding == before_adding: #<==then we either got a bunch of duplicates or we're getting very few results back.
           self.finished = True
       else:
           self.offset += len(new_results)

    def page_through_results(self, *args, **kwargs):
        while not self.finished:
            new_resp_urls = ...<call_logic>...
            self.calc_next_offset(new_resp_urls) 
            ...<save logic>...
        print(f'All unique results for q={self.q} have been obtained.')

一旦获得完整的重复响应,这^将停止分页。