2016-08-29 37 views
1

我构建了一个python 2.7 BING SEARCH API拉,每页返回50个计数,并且通过每次将偏移值改为50来进行分页。我的结果写入JSON文件。使用BING SEARCH API时,如何忽略重复结果?

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

我很担心,因为我得到了多个重复的搜索结果。当我翻页10次(因此,检索50×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() 
+0

想问BING API的专家,如果包括MSEDGE细节到我的头参数将提高返回的搜索结果的质量,如果有别的什么事可以做,将产生的结果与少重复。我做了一次搜索,其中涉及10,000个搜索结果,其中约900/10,000个是独特的(不到10%)。一些搜索结果重复多达800次。 –

+0

我可以同意,我有同样的问题。随着越来越多的页面越来越多,但不时还会有新的项目。我正在浪费大量的API调用获取重复的数据。 –

回答

0

谢谢您的查询。我们注意到您有以下分页编号:

我注意到的一件事是分页设置为10. num_paginations = 10 ## var用于控制最大分页编号。

感谢, 帕特里克

+0

谢谢你的回应。我使用num_paginations来定义对Bing执行HTTP REQUESTS的次数。所以,如果我想执行10个请求,我会将该var设置为10.我使用'pull_count'来设置我想要的搜索结果(即50个)PER请求。我的代码生成的URL对每个http请求具有不同的偏移量#。 我的请求URL都有&count = 50.对于第一个请求URL,我使用&offset = 0,然后&offset = 50,然后&offset = 100。等等。理论上,从我在文档/论坛上阅读的所有内容中,我应该很乐意去。你还有其他建议吗? –