需要帮助了解Bing Web Search API v5

时间:2016-07-03 12:00:58

标签: bing microsoft-cognitive web-search

我已使用beta version of the Bing Web Search API更新了我之前创建的以下代码段,以使用newer domain name api.cognitive.microsoft.com/bing/v5.0/search <{3}}现在使用的 -

请替换您自己的Bing API密钥以运行示例

&#13;
&#13;
<!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>
&#13;
&#13;
&#13;

我看到之前返回的结果与结果现在取得了。我想知道我的代码是否有问题导致更改的行为以及以下内容的答案:

  1. 对于某些搜索关键字,我已经看到返回的最大结果现在正好是1000(totalEstimatedMatches = 1000),但如果我搜索Bing的网站则会有更多。是1000的最大限制,是否有一些限制?
  2. fresh = Month 添加请求参数时,如果我没有使用它,会返回更多结果吗?如果未指定新鲜度,是不是获取所有结果的默认行为?

1 个答案:

答案 0 :(得分:1)

  1. Bing没有totalEstimatedMatches的最大值。虽然记住它是重要的,但我们并不知道具体的估算方法。对于我们所知道的,它可能意味着500 < totalEstimatedMatches < 2000。他们如何评估&#39;此编号不在文档中。
  2. 我在很长一段时间里盯着你的第二个问题,然后才弄清楚不和谐的地方。尝试将&responseFilter=Webpages添加到您正在制作的所有网址的末尾。列出您的回复时,您似乎只计算了网络结果,但没有指定查询只返回网络结果。