Skip to content

Instantly share code, notes, and snippets.

@mvark
Last active June 9, 2016 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvark/5d2c25d168d4b7884706b171dd3801a0 to your computer and use it in GitHub Desktop.
Save mvark/5d2c25d168d4b7884706b171dd3801a0 to your computer and use it in GitHub Desktop.
Fetching results using version 5 of Bing Search 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>
// more info: http://mvark.blogspot.com/2016/06/how-to-use-bing-search-v5-api-with.html
var total;
var ofst = 0;
var pgcnt=50;
var results = '';
// Specify the keyword you want to search as the value for the querystring key "q".
// In this sample, I'm trying to find the pages of my blog that Bing has indexed with the "site:" operator
//For more info on the other parameters refer to the documentation - https://bingapis.portal.azure-api.net/docs/services/56b43eeccf5ff8098cef3807/operations/56b4447dcf5ff8098cef380d
var burl = "https://bingapis.azure-api.net/api/v5/search/?q=site:mvark.blogspot.com&count=" + pgcnt + "&offset=" + ofst + "&mkt=en-us&freshness=Month"; //https matters for header auth
$(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; //count of total estimated matches may change drastically. why?
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);
if (total > ofst) {
ofst += pgcnt;
burl = "https://bingapis.azure-api.net/api/v5/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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment