Bing API Advanced运算符,用于使用python进行网络搜索

时间:2019-03-29 15:24:16

标签: python bing-api

我想使用高级运算符来过滤搜索结果。搜索结果应仅包含PDF。我添加了高级运算符(文件类型:pdf)。但似乎不起作用。

subscription_key = "My_ACCESS_KEY"
assert subscription_key
search_url = "https://api.cognitive.microsoft.com/bing/v7.0/search"
search_term = "NASA"
import requests

headers = {"Ocp-Apim-Subscription-Key" : subscription_key}
params  = {"q": search_term, "filetype":"pdf", "responseFilter":"Webpages", textDecorations":True, "textFormat":"HTML"}
response = requests.get(search_url, headers=headers, params=params)
response.raise_for_status()
search_results = response.json()

我不知道如何使用高级运算符( filetype:pdf )过滤搜索结果。

有人可以建议我如何使用它吗?

谢谢

1 个答案:

答案 0 :(得分:0)

我只是尝试了他们文档中的示例代码。我添加了filetype作为url查询参数,并且似乎可以正常工作。

import requests

subscription_key = "..."
assert subscription_key

search_url = "https://api.cognitive.microsoft.com/bing/v5.0/search"

search_term = "Machine%20Learning&filetype=pdf"

headers = {"Ocp-Apim-Subscription-Key" : subscription_key}

params  = {"q": search_term}
response = requests.get(search_url, headers=headers, params=params)
response.raise_for_status()
search_results = response.json()

print(search_results)