R:如何使用 R 使用 Bing 免费层网络搜索
R: How to use Bing free tier web search using R
假设用户提供了卡和 phone 并且拥有有效的 Azure 帐户。创建了免费层级服务。 (有密钥和端点,类似于 xyz.cognitiveservices.azure.com/bing/v7.0
使用免费套餐(每秒 3 个搜索者,每月最多 3 个左右)(参见此处 https://azure.microsoft.com/en-us/pricing/details/cognitive-services/)
它是 GET 还是 POST 调用以及正确的 header 参数是什么?
他们只有 Python 个不起作用的示例。
https://docs.microsoft.com/en-us/azure/cognitive-services/bing-web-search/quickstarts/python
问题是如何在 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')
对于 /search
端点,需要带有 non-empty 搜索参数 (q
) 的 GET
请求。
Basic Authentication
完全不受支持。相反,如 Python 示例所示,需要包含您的订阅密钥的 HTTP header 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
有关 header 和查询参数的详细信息,请参阅 Web Search API v7 reference。
假设用户提供了卡和 phone 并且拥有有效的 Azure 帐户。创建了免费层级服务。 (有密钥和端点,类似于 xyz.cognitiveservices.azure.com/bing/v7.0
使用免费套餐(每秒 3 个搜索者,每月最多 3 个左右)(参见此处 https://azure.microsoft.com/en-us/pricing/details/cognitive-services/)
它是 GET 还是 POST 调用以及正确的 header 参数是什么? 他们只有 Python 个不起作用的示例。 https://docs.microsoft.com/en-us/azure/cognitive-services/bing-web-search/quickstarts/python
问题是如何在 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')
对于 /search
端点,需要带有 non-empty 搜索参数 (q
) 的 GET
请求。
Basic Authentication
完全不受支持。相反,如 Python 示例所示,需要包含您的订阅密钥的 HTTP header 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
有关 header 和查询参数的详细信息,请参阅 Web Search API v7 reference。