Python Wrapper on Bing API

by lakshman on June 10, 2009

The newly launched search engine Bing has a simple restful API. We have created a thin Python wrapper over this API, which allows to query the Bing servers in a very pythonic way.

Installing this is as easy as easy_install bingapi.

[Or get it here or here ]

Using

from bingapi import bingapi
bing = bingapi.Bing(<appid>)
bing.do_web_search('Usware Technologies')

The README from the project is posted below, which provides more details on using this.

bingapi.py is a very thin python wrapper over the Bing API. Bing provides a very simple Restful interface to their search engine and provides results in JSON and XML interface.

With bingapi.py, we query the rest urls to get the JSON response, which is parsed with simplejson.py. bingapi.py just adds simple niceties like logging and error handling.

Installing bingapi.py

easy_install bingapi

or Svn it from https://svn.uswaretech.com/bingapi/

Using bingapi.py

To use this library, you need to have an Appid from Bing. You can get it from http://www.bing.com/developers/createapp.aspx

The meat of bingapi.py is talk_to_bing function. It takes a query(eg. salsa) and a sources(eg. web) and other optional parameters in extra_args (eg. {‘web.offset’: 40}) and returns the dictionary of Bing’s response.

eg

In [2]: from bingapi import bingapi

In [3]: bing = bingapi.Bing('<Your Appid>')

In [4]: bing.talk_to_bing('salsa', sources='web')
Out[4]: 
..........

In [5]: bing.talk_to_bing('salsa', sources='web', extra_args={'web.offset':40})
Out[5]: 
..........

Class Bing also provides utility functions to do the various types of seraches so you dont have to remember the source type.

The various functions available are,

do_web_search
do_image_search
do_news_search
do_spell_search
do_related_search
do_phonebook_search
do_answers_search

All of them are used similarly.

In [6]: bing.do_web_search('salsa')
Out[6]: 
..........

In [7]: bing.do_image_search('salsa')
Out[7]: 
..............

In [8]: bing.do_answers_search('what is salsa')
Out[8]: 
...............

If you want to use multiple sources in one call you can use talk_to_bing directly as

In [10]: bing.talk_to_bing(query='salsa', sources='web news')
Out[10]:
............

But, but.. Why bing? you ask. Well, other than the fact that bing search is indeed better in some areas, particularly multimedia, bing has fairer usage quota and API restrictions.

Resources


Developing a search mashup? Bing API? Yahoo, Google APIs? We can help.

Related posts:

  1. Yahoo BOSS python api
  2. Better Python package management using source and version control systems
  3. Finding keywords using Python
  4. Beginning python
  5. Python metaclasses and how Django uses them

1 Comment 3 Tweets 3 Comments

{ 3 trackbacks }

PyBing – A Python API to the Bing Search Engine « coding.
June 12, 2009 at 9:53 am
Developer Blog
June 12, 2009 at 1:37 pm
[GeeXLab] Bing, Google et Yahoo via Python « HackLAB
November 24, 2009 at 4:29 pm

{ 13 comments… read them below or add one }

1 Erik Karulf June 12, 2009 at 3:44 pm

Hi All,

Two questions 1) What is the project license? 2) Could you try importing Python 2.6’s builtin json module and fall back to simplejson ?

Thanks!

2 Lakshman June 12, 2009 at 11:03 pm

Eric,

The wrapper is Open Source and released under the liberal GPL v3 license.

3 toxicafunk June 13, 2009 at 12:21 pm

Hi,

I keep getting this error when using easy_install:

ImportError: No module named ez_setup

seems like you’re missing a file.

4 shabda June 14, 2009 at 4:17 am

toxicafunk: You can get it from here. http://peak.telecommunity.com/DevCenter/EasyInstall

5 Pablo July 22, 2009 at 9:00 am

Thanks for this tool but, is there any way to modify the language setting ? (for eg, I’m in france but sending queries in “english”… the result contains only “french”) any help ? Thanks

6 Pablo July 23, 2009 at 7:59 am

Ok, I figured it out. Just had to add “language:en” at the end of the query.

7 mdipierro June 10, 2009 at 8:24 am

The great thing about this is that the code (bingapi.py) consists of 64 lines total of pure python code with no dependencies. Works out of the box with any Python code.

This comment was originally posted on Reddit

8 jimmybot June 10, 2009 at 8:28 am

Haven’t been testing out Bing like some of the others out there. Just took a quick look at Bing news search–they incorporate news results with top blogs? That seems interesting to me. Is that unique, anyone know? I don’t see it in Google News.

This comment was originally posted on Hacker News

9 jerf June 10, 2009 at 11:43 am

OK, great, but can we please avoid the "Bing API for X" flurry from here on out? Yes, I’m sure there will soon be Bing APIs for Clojure, Haskell, Perl, PHP, C, C++, and so on and so forth.If I want them, I’ll Google for them.

This comment was originally posted on Hacker News

10 jmatt June 10, 2009 at 12:08 pm

Agreed.As for the actual api it’s here:

http://msdn.microsoft.com/en-us/library/dd251056.aspx

This comment was originally posted on Hacker News

Leave a Comment

Additional comments powered by BackType

Previous post:

Next post: