R:如何使用R使用Bing免费网络搜索

问题描述 投票:1回答:1

假设用户提供了卡和电话,并且具有有效的Azure帐户。创建了免费套餐服务。 (具有密钥和端点,例如xyz.cognitiveservices.azure.com/bing/v7.0

使用免费套餐(每秒3名搜索者,每月最多搜索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个回答
0
投票

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

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。>>

© www.soinside.com 2019 - 2024. All rights reserved.