0

我正在使用 Bing Web Search API 在 Bing.com 中搜索一些信息。但是,对于某些搜索查询,我从搜索中获取了与我的目的无关的网站。

例如,如果搜索查询是今年夏天要阅读的书籍, Bing 有时会返回youtube.comamazon.com作为结果。但是,我不想在我的搜索结果中出现这类网站。所以,我想在搜索开始之前屏蔽这些类型的网站。

这是我的示例代码:

    params = {
        "mkt": "en-US",
        "setLang": "en-US",
        "textDecorations": False,
        "textFormat": "raw",
        "responseFilter": "Webpages",
        "q": "books to read this summer",
        "count": 20
    }

    headers = {
        "Ocp-Apim-Subscription-Key": MY_APY_KEY_HERE,
        "Accept": "application/json",
        "Retry-After": "1",
    }


    response = requests.get(WEB_SEARCH, headers=headers, params=params, timeout=15)

    response.raise_for_status()

    search_data = response.json()

search_data变量包含我想要阻止的链接,而不需要对响应对象进行迭代。特别是,我希望 Bing 不要在结果中包含 youtube 和 amazon。

任何帮助将不胜感激。

编辑:WEB_SEARCH是网络搜索端点

4

1 回答 1

2

在挖掘 Bing Web Search 文档后,我找到了答案。这里是。

LINKS_TO_EXCLUDE = ["-site:youtube.com", "-site:amazon.com"]


def bing_data(user_query):

    params = {
        "mkt": "en-US",
        "setLang": "en-US",
        "textDecorations": False,
        "textFormat": "raw",
        "responseFilter": "Webpages",
        "count": 30
    }

    headers = {
        "Ocp-Apim-Subscription-Key": MY_APY_KEY_HERE,
        "Accept": "application/json",
        "Retry-After": "1",
    }
    
    # This is the line what I needed to have
    params["q"] = user_query + " " + " ".join(LINKS_TO_EXCLUDE)

    response = requests.get(WEB_SEARCH_ENDPOINT, headers=headers, params=params)
    response.raise_for_status()
    search_data = response.json()

    return search_data
于 2021-10-10T12:45:17.390 回答