我可以同时使用必应搜索 API 和自动建议 API 吗?

Can I use Bing Search API and Auto Suggest API at the same time?

提问人:Zhe Lin 提问时间:7/22/2023 更新时间:7/22/2023 访问量:148

问:

我已经使用 Bing 搜索 API 很长时间了,现在我希望在我的程序中添加 Bing AutoSuggest API,因此我输入与搜索 API 相同的订阅密钥来获取 AutoSuggest 结果。但是程序给出了一个错误:

{
    "error": {
        "code": "401",
        "message": "Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource."
    }
}

是否需要在 Azure 中创建另一个资源组才能进行自动建议?但是,当我这样做时,Azure在部署时显示配额不足的错误消息:

    {
    "status": "Failed",
    "error": {
        "code": "InsufficientQuota",
        "message": "Insufficient Quota"
    }
}

我现在应该怎么做才能使自动推荐可用?顺便说一句,我正在为学生使用 Azure 帐户。

Azure 必应 bing-api

评论


答:

0赞 Venkatesan 7/22/2023 #1

我可以同时使用必应搜索 API 和自动建议 API 吗?

是的,您可以同时使用必应搜索 API 和自动建议 API。

可以使用以下 Python 代码同时获取必应搜索 API 和自动建议 API。

法典:

import http.client,json,requests

subscriptionKey = 'Your-key'
host = 'api.bing.microsoft.com'
path = '/v7.0/Suggestions'  
search_url = "https://api.bing.microsoft.com/v7.0/search"
mkt = 'en-US'
query = 'sai' 
search_term = "water"

params = '?mkt=' + mkt + '&q=' + query
headers = {'Ocp-Apim-Subscription-Key': subscriptionKey} 

headers1 = {"Ocp-Apim-Subscription-Key": subscriptionKey}
params1 = {"q": search_term, "textDecorations": True, "textFormat": "HTML"}
response1 = requests.get(search_url, headers=headers1, params=params1)
response1.raise_for_status()
search_results = response1.text
print(json.dumps(json.loads(search_results), indent=4))

conn = http.client.HTTPSConnection(host)
conn.request ("GET", path + params, None, headers)
response = conn.getresponse ()
Auto_result = response.read ()  
print("Autosuggest",json.dumps(json.loads(Auto_result), indent=4))

示例输出:

{
    "_type": "SearchResponse",
    "queryContext": {
        "originalQuery": "water"
    },
    "webPages": {
        "webSearchUrl": "https://www.bing.com/search?q=water",
        "totalEstimatedMatches": 80500000,
        "value": [
            {
                "id": "https://api.bing.microsoft.com/api/v7/#WebPages.0",
                "contractualRules": [
                    {
                        "_type": "ContractualRules/LicenseAttribution",
                        "targetPropertyName": "snippet",
                        "targetPropertyIndex": 0,
                        "mustBeCloseToContent": true,
                        "license": {
                            "name": "CC-BY-SA",
                            "url": "http://creativecommons.org/licenses/by-sa/3.0/"
                        },
                        "licenseNotice": "Text under CC-BY-SA license"
                    }
                ],
                "name": "Water - Wikipedia",
                "url": "https://en.wikipedia.org/wiki/Water",
                "thumbnailUrl": "https://www.bing.com/th?id=OIP.lqPjgDbDBZliHqXtjKciCQHaE3&w=80&h=80&c=1&pid=5.1",
                "isFamilyFriendly": true,
                "displayUrl": "https://<b>en.wikipedia.org</b>/wiki/<b>Water</b>",
                "snippet": "<b>Water</b> is an inorganic compound with the chemical formula H 2 O. It is a transparent, tasteless, odorless, and nearly colorless chemical substance, and it is the main constituent of Earth&#39;s hydrosphere and the fluids of all known living organisms (in which it acts as a solvent). It is vital for all known forms of life, despite not providing food energy, or organic micronutrients.",
                "dateLastCrawled": "2023-07-21T04:14:00.0000000Z",
                "language": "en",
                "isNavigational": false
            },
            {
                "id": "https://api.bing.microsoft.com/api/v7/#WebPages.1",
                "name": "Water | Definition, Chemical Formula, Structure, Molecule, &amp; Facts ...",
                "url": "https://www.britannica.com/science/water",
                "thumbnailUrl": "https://www.bing.com/th?id=OIP.8FrlGPSeeeTBA6ZErHMl4AHaE7&w=80&h=80&c=1&pid=5.1",
                "isFamilyFriendly": true,
                "displayUrl": "https://<b>www.britannica.com</b>/science/<b>water</b>",
                "snippet": "Liquid <b>water</b>. <b>water</b> molecule. The <b>water</b> molecule is composed of two hydrogen atoms, each linked by a single chemical bond to an oxygen atom. Most hydrogen atoms have a nucleus consisting solely of a proton. Two isotopic forms, deuterium and tritium, in which the atomic nuclei also contain one and two neutrons, respectively, are found to a small ...",       
                "dateLastCrawled": "2023-07-20T16:26:00.0000000Z",
                "language": "en",
                "isNavigational": false
            }
   } 
  Autosuggest {
"_type": "Suggestions",
"queryContext": {
    "originalQuery": "sai"
},
"suggestionGroups": [
    {
        "name": "Web",
        "searchSuggestions": [
            {
                "url": "https://www.bing.com/search?q=sai+pallavi&FORM=USBAPI",
                "displayText": "sai pallavi",
                "query": "sai pallavi",
                "searchKind": "WebSearch"
            },
            {
                "url": "https://www.bing.com/search?q=sai+baba&FORM=USBAPI",
                "displayText": "sai baba",
                "query": "sai baba",
                "searchKind": "WebSearch"
            },
            {
                "url": "https://www.bing.com/search?q=saina+nehwal&FORM=USBAPI",
                "displayText": "saina nehwal",
                "query": "saina nehwal",
                "searchKind": "WebSearch"
            },
            {
                "url": "https://www.bing.com/search?q=sail+share+price&FORM=USBAPI",
                "displayText": "sail share price",
                "query": "sail share price",
                "searchKind": "WebSearch"
            },
            {
                "url": "https://www.bing.com/search?q=saif+ali+khan&FORM=USBAPI",
                "displayText": "saif ali khan",
                "query": "saif ali khan",
                "searchKind": "WebSearch"
            },
            {
                "url": "https://www.bing.com/search?q=sailpoint&FORM=USBAPI",
                "displayText": "sailpoint",
                "query": "sailpoint",
                "searchKind": "WebSearch"
            },
            {
                "url": "https://www.bing.com/search?q=saint-gobain&FORM=USBAPI",
                "displayText": "saint-gobain",
                "query": "saint-gobain",
                "searchKind": "WebSearch"
            },
            {
                "url": "https://www.bing.com/search?q=saira+banu&FORM=USBAPI",
                "displayText": "saira banu",
                "query": "saira banu",
                "searchKind": "WebSearch"
            }
        ]
    }
]}

enter image description here

参考:

  1. 快速入门:使用 Python 执行搜索 - 必应 Web 搜索 API - 必应搜索服务 |Microsoft 学习
  2. 快速入门:使用必应自动推荐 REST API 和 Python 建议搜索字符串 - 必应搜索服务 |Microsoft 学习