6

How can I search Bing for images using key words?

I am able to 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 it'd start 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 the credentials set up?

jensph
  • 763
  • 1
  • 10
  • 22

4 Answers4

4

The Bing equivalent would be:

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

jensph
  • 763
  • 1
  • 10
  • 22
  • 1
    Apparently the URL pattern has changed somewhat ... I kept getting a 403, until I put `/v1` in there: e.g. `https://api.datamarket.azure.com/Bing/SearchWeb/v1/Web?Query=...` This is not in the documentation but in the dataset explorer, e.g. https://datamarket.azure.com/dataset/8818F55E-2FE5-4CE3-A617-0B8BA8419F65 Also note that you have to not only sign up for Azure Marketplace, you also have to sign up for the relevant data service, e.g. Search or SearchWeb. – LarsH May 05 '15 at 01:35
  • guguncube uses `[:-1]` after the base64-encoding of the bing key, to remove a trailing newline. I had to do that too (though I used `.rstrip()`). Why do you have `[:]`? Doesn't that just make a copy of the string? – LarsH May 05 '15 at 01:35
  • 1
    I don't remember why I removed the -1, but at the time I used the above it was working... Thanks for the URL update. – jensph May 05 '15 at 13:04
2

In Python 3.0+, this looks like:

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 off my web search function here.

Alex P. Miller
  • 2,128
  • 1
  • 23
  • 20
2

There's a python package named PyBingSearch for that (ok I admit it, I wrote a chunk of the package).

To Install:

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'll need to get the API key from Bing (free up to 5k/month). The package allows you to search for web, image, video and news.

Now works for Python3.* as well, just install with:

pip3 install py-bing-search

There's now a new Microsoft Cognitive Service which takes over the old API. The new python package that can help you with that is PyMsCognitive.

Tristan Tao
  • 855
  • 11
  • 29
  • @Tristen Tao the module seems great but when I try and run the line that declares `first_fifity_result` I get an error stating `Request returned with code 401, error msg: The authorization type you provided is not supported. Only Basic and OAuth are supported` – Jstuff Jul 21 '16 at 15:17
  • @Jstuff feel free to send a bug report on the github issues page. Please include your module version + python version, the exact code (without your auth obviously) and I'll take a look :) – Tristan Tao Jul 21 '16 at 15:40
  • @Tristen Tao Any chance we can discuss it here? I'm at work and can't log onto github. I'm using python 3.5 and simply trying to use the example in https://github.com/tristantao/py-bing-search to return web search results and get the error I mentioned above on the line mentioned above. – Jstuff Jul 21 '16 at 15:48
  • @Jstuff I can't seem to reproduce it; all the search tests are passing for me. There are a couple of things that can be causing it. But before we dive into code, as an clumsy engineer myself, I have to ask: did you copy and paste the credentials 100% correctly? – Tristan Tao Jul 21 '16 at 16:36
1

The Bing equivalent for python 3:

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

Shahryar
  • 1,454
  • 2
  • 15
  • 32