azure 我可以同时使用Bing Search API和Auto Suggest API吗?

xxe27gdn  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(115)

我已经使用Bing搜索API很长一段时间了,现在我希望在我的程序中添加Bing自动建议API,所以我输入与搜索API相同的订阅密钥来获得自动建议结果。但程序给出了一个错误:

{
    "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的学生帐户。

mkh04yzy

mkh04yzy1#

我可以同时使用Bing Search API和Auto Suggest API吗?
是的,您可以同时使用Bing搜索API和自动建议API。
您可以使用下面的Python代码同时获得Bing搜索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"
            }
        ]
    }
]}


的数据

参考号:

  1. Quickstart: Perform a search with Python - Bing Web Search API - Bing Search Services | Microsoft Learn
  2. Quickstart: Suggest search strings with Bing Autosuggest REST API and Python - Bing Search Services | Microsoft Learn

相关问题