使用 BING SEARCH API 时,如何省略重复结果?

Posted

技术标签:

【中文标题】使用 BING SEARCH API 时,如何省略重复结果?【英文标题】:When using BING SEARCH API, how do you omit duplicate results? 【发布时间】:2016-08-29 23:28:38 【问题描述】:

我构建了一个 python 2.7 BING SEARCH API pull,它每页返回 50 个计数,并通过每次将偏移值更改为 50 来进行分页。我的结果被写入一个 JSON 文件。

我在我的 api 调用的标头中指定了 User-Agent 和 X-Search-ClientIP。我还指定了网页的 responseFilter,以及“en-us”的 mkt 值。

我很担心,因为我得到了几个重复的搜索结果。当我翻页 10 次(因此,检索 50 X 10 = 500 个结果)时,其中大约 17% 是重复记录。有没有一种方法可以强制 bing 只返回非重复值?您建议我采取哪些额外步骤,以尽可能接近仅取回唯一值?

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import httplib, urllib, base64, json, re, os, sys, codecs, locale, time

pull_count = 50                            ## Var used to control # of hits per pull
offset = 0                                 ## Var used to push pagination counter to http get
num_paginations = 10                       ## Var used to control max # of paginations
local_counter = 1                          ## Helps Write commas to json file for all but last run
timer_counter = 1                          ## Variable used to make system wait after 5 pulls
dump_file = 'BingDump.json'                ## Name of local file where results are written to
api_domain = 'api.cognitive.microsoft.com'
query = 'Bill Gates'
user_agent = 'Mozilla/5.0 (MAC OSX, Educational Usage Only)'
x_search = '199.99.99.99'

#Request Headers, open connection, open file write to output file
headers = 
    'Ocp-Apim-Subscription-Key': 'MYSUBSCRIPTIONKEY',
    'User-Agent' : user_agent,
    'X-Search-ClientIP': x_search,

conn = httplib.HTTPSConnection(api_domain)
fhand = open(dump_file,'w')

#Function to build URL for API PULL
def scraper() : 
    pull_count_str = str(pull_count)
    offset_str = str(offset)
    params = urllib.urlencode(
        'q': query,
        'count': pull_count_str,
        'offset': offset_str,
        'mkt': 'en-us',
        'safesearch': 'Moderate',
        'responseFilter': 'webpages', #controls whether pull scrapes from web/image/news etc
    )
    return(params)

#Function set to wait 4 seconds after 5 pulls
def holdup(entry) : 
    if entry != 5 : 
        entry += 1
    else: 
        entry = 1
        time.sleep(4)
    return(entry)

#Function that establishes http get, and writes data to json file
def getwrite(entry1, entry2) : 
    conn.request("GET", "/bing/v5.0/search?%s" % entry1, "body", entry2)
    response = conn.getresponse()
    data = response.read()
    json_data = json.loads(data)
    fhand.write(json.dumps(json_data, indent=4)) 

#Main Code - Pulls data iteratively and writes it to json file
fhand.write('') 

for i in range(num_paginations) : 

    dict_load = '"' + str(local_counter) + '"' +  ' : '
    fhand.write(dict_load)
    try:
        link_params = scraper()    
        print('Retrieving: ' + api_domain + '/bing/v5.0/search?' + link_params)
        getwrite(link_params, headers)
    except Exception as e:
        print("[Errno 0] 1".format(e.errno, e.strerror))
        fhand.write('"Error. Could not pull data"')        
    offset += pull_count
    if local_counter != num_paginations : fhand.write(', ') 
    local_counter += 1
    timer_counter = holdup(timer_counter)

fhand.write('')
fhand.close()
conn.close()

【问题讨论】:

想询问 BING API 专家,是否将 MSEDGE 详细信息包含在我的标头参数中会提高返回的搜索结果的质量,以及是否可以采取任何其他措施来产生更少重复的结果。我做了一个涉及 10,000 个搜索结果的拉动,其中大约 900/10,000 个左右是唯一的(不到 10%)。有些搜索结果重复了多达 800 次。 我同意,我也有同样的问题。随着我获得的页面越来越多,被骗的也越来越多,但仍然不时有新项目。我浪费了很多获取重复数据的 api 调用。 【参考方案1】:

感谢您的询问。我们注意到您的分页数如下:

我注意到的一件事是分页设置为 10。

num_paginations = 10 ## Var used to control max # of paginations.

【讨论】:

感谢您的回复。我正在使用 num_paginations 来定义针对 Bing 执行 HTTP 请求的次数。所以如果我想执行 10 个请求,我会将该 var 设置为 10。我使用“pull_count”来设置我想要的搜索结果数(即 50)PER 请求。我的代码生成的 URL 具有不同的偏移量 # 每个 http 请求。我的请求 URL 都有 &count = 50。对于第一个请求 URL,我使用 &offset=0,然后是 &offset = 50,然后是 &offset=100。等等。理论上,从我在文档/论坛上阅读的所有内容来看,我应该很高兴。你还有什么建议我做的吗?

以上是关于使用 BING SEARCH API 时,如何省略重复结果?的主要内容,如果未能解决你的问题,请参考以下文章