,

Python – Using the Bing Search API

Introduction

Bing is a great search engine with very generous API volumes for developers.
Currently 5.000 queries per month can be called for free. Compare with Google: 100 per day * 30 = 3.000 per month.

There a couple of Python libraries which used to work with Bing API.
I tried pybing but the the authentification method used there is outdated, therefore my API key rejected.
bingsearch on the other hand only supports Web Search which is too narrow.

The only luck I got was using some code from Trinstan.

In order to use Bing Search API, you need to register at a Microsoft portal (live/hotmail/…)
See: Bing Development Center

Thereafter at datamarket.azure.com, Retrieve the API Key through Account -> Keys
Bing API Keys

Python Script for Bing Search


# -*- coding: utf-8 -*-
import urllib
import urllib2
import json

def main():
    query = "sunshine"
    print bing_search(query, 'Web')
    print bing_search(query, 'Image')

def bing_search(query, search_type):
    #search_type: Web, Image, News, Video
    key= 'YOUR_API_KEY'
    query = urllib.quote(query)
    # create credential for authentication
    user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; FDM; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 1.1.4322)'
    credentials = (':%s' % key).encode('base64')[:-1]
    auth = 'Basic %s' % credentials
    url = 'https://api.datamarket.azure.com/Data.ashx/Bing/Search/'+search_type+'?Query=%27'+query+'%27&$top=5&$format=json'
    request = urllib2.Request(url)
    request.add_header('Authorization', auth)
    request.add_header('User-Agent', user_agent)
    request_opener = urllib2.build_opener()
    response = request_opener.open(request)
    response_data = response.read()
    json_result = json.loads(response_data)
    result_list = json_result['d']['results']
    print result_list
    return result_list

if __name__ == "__main__":
    main()

References

1. Trinstan Bing API: http://xiaoyao-web-hacks.blogspot.de/2012/09/use-python-to-access-windows-azure-bing.html
2. Development Center: http://datamarket.azure.com/dataset/bing/search
3. Bing API Keys: https://datamarket.azure.com/account/keys
4. Bing Webmaster Tools: https://ssl.bing.com/webmaster/home/mysites
5. Python Bing Wrapper: http://agiliq.com/blog/2009/06/python-wrapper-on-bing-api/
6. pyBing: https://code.google.com/p/pybing/
7. BingSearch :https://github.com/guitarparty/bingsearch