Created
March 18, 2013 07:47
-
-
Save thinkphp/5185651 to your computer and use it in GitHub Desktop.
A simple client for Microsoft's Bing API Search.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import urllib | |
| import urllib2 | |
| import json | |
| from xml.dom.minidom import parse | |
| class Bing: | |
| ENDPOINT = 'http://api.bing.net/format.aspx' | |
| def __init__(self, appid, format='json'): | |
| self.appid = appid | |
| self.format = format | |
| def search(self,**params): | |
| self.ENDPOINT = self.ENDPOINT.replace('format',self.format) | |
| data = {} | |
| for k,v in params.items(): | |
| data.update({k.replace('_','.'):v.replace(' ','+')}) | |
| qs = urllib.urlencode(data).replace('%2B','+') | |
| url = '%s?Appid=%s&%s' % (self.ENDPOINT, self.appid, qs) | |
| f = urllib2.urlopen(url) | |
| if self.format == 'json': | |
| return json.loads(f.read()) | |
| else: | |
| return parse(f) | |
| bing = Bing('your-api-key') | |
| print bing.search(query='MooTools',sources='web') | |
| print bing.search(query='jQuery',sources='web image') | |
| print bing.search(query='Dojo',sources='news') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment