Bing search through Azure API using Python

How can I search for Bing for images using keywords?

I can search Google using:

import urllib2 import json credentialGoogle = '' # Google credentials from: https://console.developers.google.com/ searchString = 'Xbox%20One' top = 20 offset = 0 while offset < top: url = 'https://ajax.googleapis.com/ajax/services/search/images?' + \ 'v=1.0&q=%s&start=%d&userip=%s' % (searchString, offset, credentialGoogle) request = urllib2.Request(url) response = urllib2.urlopen(request) results = json.load(response) # process results offset += 4 # since Google API only returns 4 search results at a time 

What would be the equivalent for Bing? Presumably this started with:

 keyBing = '' # Bing key from: https://datamarket.azure.com/account/keys credentialBing = '' # same as key? searchString = '%27Xbox+One%27' top = 20 offset = 0 url = 'https://api.datamarket.azure.com/Bing/Search/Image?' + \ 'Query=%s&$top=%d&$skip=%d&$format=json' % (searchString, top, offset) 

but how are credentials configured?

+6
source share
4 answers

Bing equivalent:

 keyBing = '' # get Bing key from: https://datamarket.azure.com/account/keys credentialBing = 'Basic ' + (':%s' % keyBing).encode('base64')[:-1] # the "-1" is to remove the trailing "\n" which encode adds searchString = '%27Xbox+One%27' top = 20 offset = 0 url = 'https://api.datamarket.azure.com/Bing/Search/Image?' + \ 'Query=%s&$top=%d&$skip=%d&$format=json' % (searchString, top, offset) request = urllib2.Request(url) request.add_header('Authorization', credentialBing) requestOpener = urllib2.build_opener() response = requestOpener.open(request) results = json.load(response) # process results 

Solution thanks to http://www.guguncube.com/2771/python-using-the-bing-search-api

+4
source

In Python 3.0+, it looks like this:

 from urllib.parse import quote_plus import json import requests def bing_search(query): # Your base API URL; change "Image" to "Web" for web results. url = "https://api.datamarket.azure.com/Bing/Search/v1/Image" # Query parameters. Don't try using urlencode here. # Don't ask why, but Bing needs the "$" in front of its parameters. # The '$top' parameter limits the number of search results. url += "?$format=json&$top=10&Query=%27{}%27".format(quote_plus(query)) # You can get your primary account key at https://datamarket.azure.com/account r = requests.get(url, auth=("","YOUR_AZURE_API_PRIMARY_ACCOUNT_KEY")) resp = json.loads(r.text) return(resp) 

This is based on my web search feature here .

+2
source

In this python package called PyBingSearch (I agree, I wrote a fragment of the package).

For installation:

 pip install py-bing-search 

In Python 2. *. *:

 from py_bing_search import PyBingImageSearch bing_image = PyBingImageSearch('Your-Api-Key-Here', "x-box console") first_fifty_results = bing_image.search(limit=50, format='json') #1-50 print (first_fifty_results[0].media_url) 

You need to get an API key from Bing (free up to 5k / month). The package allows you to search for web pages, images, videos and news.

Now works for Python3. * Also, just install with:

 pip3 install py-bing-search 

Now there is a new Microsoft Cognitive Service that takes over the old API. The new python package that can help you is PyMsCognitive .

+2
source

python 3's Bing equivalent:

 import http.client, urllib.request, urllib.parse, urllib.error, base64 headers = { # Request headers 'Ocp-Apim-Subscription-Key': '{subscription key}', } params = urllib.parse.urlencode({ # Request parameters 'q': 'bill gates', 'count': '10', 'offset': '0', 'mkt': 'en-us', 'safesearch': 'Moderate', }) try: conn = http.client.HTTPSConnection('api.cognitive.microsoft.com') conn.request("GET", "/bing/v5.0/search?%s" % params, "{body}", headers) response = conn.getresponse() data = response.read() print(data) conn.close() except Exception as e: print("[Errno {0}] {1}".format(e.errno, e.strerror)) 

Subscription Key can be found here

+1
source

Source: https://habr.com/ru/post/980043/


All Articles