Wednesday, July 2, 2014

Small Script to Fetch Bing search results

I have been playing with Tableau software for some data visualization practices. Thought it might be interesting if I can pull some Internet data. Looked around for some examples, and found a good one on http://www.guguncube.com/2771/python-using-the-bing-search-api. Unfortunately it seems that Bing changed its base URL and some of the minor formatting. Some quick edits was all it took to get the script up and running.

It is very nice that Bing gives you 50,000 queries per month for free. You can get your API key here, https://datamarket.azure.com/.

Here is the script:

#!/usr/bin/env python
# Modified from http://www.guguncube.com/2771/python-using-the-bing-search-api
#
# by Eric Chou
# eric@pythonicneteng.com

import urllib
import urllib2
import json
def main():
    query = "Python Newtork Engineering"
    response = bing_search(query, limit=10)
    #Raw print json.loads(response)['d']['results']
    #Title
    print "The top 10 results for your query " + query + ":"
    for i in json.loads(response)['d']['results']:
        print i['Title']

def bing_search(query, limit=5, **kwargs):
    key = 'YOURKEY'
    baseURL = 'https://api.datamarket.azure.com/Bing/SearchWeb/'
    limit = str(limit)
    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 = baseURL+'Web?Query=%27'+query+'%27&$top='+limit+'&$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()
    return response_data
if __name__ == "__main__":
    main()
Here is the output of the script:

$ python bingSearch_v1.py
The top 10 results for your query Python Newtork Engineering:
Python for Network Engineering | Facebook
Python for Network Engineers
Python for Network Engineers
Jeremy Filliben: Python Programming and the Network Engineer
Show 176 - Intro to Python & Automation for Network ...
Python For Network Engineers | Networking-Link
First Post! | Python for Network Engineers
Python For Scientific Engineering - OpenOpt
World News - Python Engineer
Network Test Engineer Python Jobs, Employment | Indeed.com

Happy Coding!

No comments:

Post a Comment