我已经使用Bing Web Search API 的 beta 版本更新了我之前创建的以下代码段,以使用更新的域名:Bing Web Search API现在使用的api.cognitive.microsoft.com/bing/v5.0/search -
请替换您自己的 Bing API 密钥以运行示例
<!DOCTYPE html>
<html>
<head>
<title>Bing Search v5 - show all results</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<script>
var total;
var ofst = 0;
var pgcnt=50;
var results = '';
var burl = "https://api.cognitive.microsoft.com/bing/v5.0/search/?q=Bill Gates&count=" + pgcnt + "&offset=" + ofst + "&mkt=en-us&freshness=Month"; //737 results
//var burl = "https://api.cognitive.microsoft.com/bing/v5.0/search/?q=Bill Gates&count=" + pgcnt + "&offset=" + ofst + "&mkt=en-us"; //304 results
//var burl = "https://api.cognitive.microsoft.com/bing/v5.0/search/?q=site:mvark.blogspot.com&count=" + pgcnt + "&offset=" + ofst + "&mkt=en-us&freshness=Month";
$(function() {
function xhr_get(url) {
return $.ajax({
url: url,
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","abc123"); //replace value with your own key
},
type: "GET",
})
.done(function(data) {
total = data.webPages.totalEstimatedMatches;
len = data.webPages.value.length
for (i=0; i<len; i++ )
{
results += "<p><a href='" + data.webPages.value[i].url + "'>" + data.webPages.value[i].name + "</a>: " + data.webPages.value[i].snippet + "</p>";
}
$('#content').html(results);
ofst += pgcnt;
if (total > ofst) {
burl = "https://api.cognitive.microsoft.com/bing/v5.0/search/?q=Bill Gates&count=" + pgcnt + "&offset=" + ofst + "&mkt=en-us&freshness=Month";
//burl = "https://api.cognitive.microsoft.com/bing/v5.0/search/?q=Bill Gates&count=" + pgcnt + "&offset=" + ofst + "&mkt=en-us";
//burl = "https://api.cognitive.microsoft.com/bing/v5.0/search/?q=site:mvark.blogspot.com&count=" + pgcnt + "&offset=" + ofst + "&mkt=en-us&freshness=Month";
xhr_get(burl);
}
else { console.log('No more results to show');
}
})
}
xhr_get(burl);
});
</script>
Results: <div id="content">Fetching...</div>
</body>
</html>
我看到之前返回的结果与现在获取的结果之间存在差异。我想知道我的代码是否有问题导致行为改变以及以下问题的答案:
- 对于某些搜索关键字,我发现返回的最大结果现在正好是 1000(totalEstimatedMatches=1000),但如果我通过 Bing 的网站搜索,结果会更多。1000 是最大限制吗?是否有一些限制?
- 当添加freshness=Month请求参数时,如果我不使用它,它会返回更多的结果吗?如果未指定新鲜度,默认行为不是获取所有结果吗?