Azure Cognitive Service – Bing Search with Python
What is Bing Web Search?
The Bing Web Search API is a RESTful service that provides instant answers to user queries. Search results are easily configured to include web pages, images, videos, news, translations, and more. Bing Web Search provides the results as JSON based on search relevance and your Bing Web Search subscriptions.
Reference : Microsoft Document
Step 1 : Create Bing Web Search Cognitive Service.
Step 2 : Implement Bing Web Search Cognitive service in Python Application.
I am using Visual studio, you can use any IDE, Also i am going use Flask so to demo Bing Search.
Create Web Flask Project in visual studio, it will create Flask project with all required libraries.
Also, we can continue working on same project, which we developed here for Bing Auto Suggest.
I am adding one more button on index screen to which open Bing Web Search Screen.

Below is the HTML for Home page/index screen
{% extends "layout.html" %}
{% block content %}
<br />
<div class="panel panel-default">
<div class="panel-heading">Web search Cognitive Services.</div>
<div class="panel-body">
<a class="btn btn-primary" href="{{ url_for('LoadBingAutosuggestForm') }}">Bing Autosuggest.</a>
<a class="btn btn-primary" href="{{ url_for('LoadBingWebSearchForm') }}">Bing Web Search.</a>
</div>
</div>
{% endblock %}
I have highlighted which line i added as a part of bing web search button.
Adding route to Web Search html page in python.
@app.route('/LoadBingWebSearchForm')
def LoadBingWebSearchForm():
return render_template(
'BingWebSearch.html',
title='Azure Bing Web Search'
)
Creating BingWebSearch.html in Template folder with following HTML
{% extends "layout.html" %}
{% block content %}
<div class="panel panel-default row">
<div class="panel-heading">{{ title }}.</div>
<div>
<input type="text" id="searchBox" /> <button onclick="CallBingWebSearchAPI(this)">Search on web</button>
<ul class="list-group" id="searchResut">
</ul>
</div>
</div>
<script type="text/javascript" charset="utf-8">
function CallBingWebSearchAPI(t) {
var url = "/CallBingWebSearchAPI";
var data = { searchq: document.getElementById("searchBox").value };
$.post(url, data, function (response) {
document.getElementById("searchResut").innerHTML = "";
if (response.videos != undefined && response.videos.value.length > 0)
response.videos.value.forEach(function (item) {
document.getElementById("searchResut").innerHTML
+= '<li> Video Name ' + item.name + ' </li>';
});
if (response.images != undefined && response.images.value.length > 0)
response.images.value.forEach(function (item) {
document.getElementById("searchResut").innerHTML
+= '<li> Image Name ' + item.name + ' </li>';
});
if (response.webPages != undefined && response.webPages.value.length > 0)
response.webPages.value.forEach(function (item) {
document.getElementById("searchResut").innerHTML
+= '<li> webPages Name ' + item.name + ' </li>';
});
if (response.news != undefined && response.news.value.length > 0)
response.news.value.forEach(function (item) {
document.getElementById("searchResut").innerHTML
+= '<li> news Name ' + item.name + ' </li>';
});
});
}
</script>
{% endblock %}
Now, in Views.py Add following to route which will triggered on click of Search on web button.
@app.route('/CallBingWebSearchAPI', methods=['POST'])
def CallBingWebSearchAPI():
subscriptionKey = '0681ef48fcda4f80a09cba8a2515cee1'
search_url = "https://api.cognitive.microsoft.com/bing/v7.0/search/"
headers = {'Ocp-Apim-Subscription-Key': subscriptionKey}
query = urllib.parse.quote(request.form['searchq'])
mkt = 'en-US'
params = { 'q': query, 'mkt': mkt }
response = requests.get(search_url,headers=headers ,params=params)
return response.json();
And the response is as below.

Hope it helps.
Thanks and please post for any queries or questions.

Like
Report
*This post is locked for comments