【问题标题】:R: How to use Bing free tier web search using RR:如何使用 R 使用 Bing 免费层网络搜索
【发布时间】:2026-02-05 07:35:01
【问题描述】:

假设用户提供了卡和电话并拥有有效的 Azure 帐户。创建了免费层级服务。 (有密钥和端点,类似于 xyz.cognitiveservices.azure.com/bing/v7.0

使用免费套餐(每秒 3 个搜索者,每月最多)(见此处https://azure.microsoft.com/en-us/pricing/details/cognitive-services/

是 GET 还是 POST 调用,正确的标头参数是什么? 他们只有不工作的 Python 示例。 https://docs.microsoft.com/en-us/azure/cognitive-services/bing-web-search/quickstarts/python

https://github.com/Azure-Samples/cognitive-services-REST-api-samples/blob/master/python/Search/BingWebSearchv7.py

问题是如何在 R 中做到这一点。

此代码不起作用

library(httr)
token='xxxxx'
server='https://xxxxx.cognitiveservices.azure.com/bing/v7.0/'
url=paste0(server,'search')
response = GET(url = url, 
               authenticate('',token, type = 'basic'))
response
res = content(response, encoding = 'json')

【问题讨论】:

    标签: r bing


    【解决方案1】:

    对于 /search 端点,需要带有非空搜索参数 (q) 的 GET 请求。

    Basic Authentication 根本不受支持。相反,如 Python 示例中所示,需要包含您的订阅密钥的 HTTP 标头 Ocp-Apim-Subscription-Key

    所以,我成功使用了以下代码。它也应该对你有用。

    library(httr)
    
    server = "https://xxxxx.cognitiveservices.azure.com/bing/v7.0/"
    token = "subscription key for Bing Search APIs v7"
    search_term = "search term"
    url = paste0(server, "search")
    
    response = GET(url = url, 
        query = list(q = search_term), 
        add_headers(`Ocp-Apim-Subscription-Key` = token)
    )
    res = content(response, encoding = "json")
    res
    

    有关标头和查询参数的更多信息,请参阅Web Search API v7 reference

    【讨论】:

    • 谢谢。结果计数可以通过res$webPages$totalEstimatedMatches 看到,也不错的是jsonview::json_tree_view(res)