In my previous post, I have explained how we can use the Twitter API with Python to perform user based operations such as update Twitter status, check your home timeline, etc. Based on a similar method of using web services, we can query the Google search engine and get the search results using a Python script.
The Google search engine can be accessed by JavaScript using JQuery. However, non JavaScript users can query the Google search engine using the webservice
http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="Your query string here"
The returned format is JSON(Javascript Object Notation).
The following Python script uses the http GET request to query the word “Kevin Rodrigues” in the Google search engine.
import urllib
import httplib2
import base64
import json
def google_search(phrase):
"""Get google search results for the phrase"""
url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&" + urllib.parse.urlencode({'q':phrase});
referer = "http://kevinrodrigues.com"
h = httplib2.Http(".cache")
resp, content = h.request(url, "GET", headers={'Referer': referer})
if resp.status == 200:
str_content = str(content)[2:-1]
search_result = json.loads(str_content)
print(search_result)
else:
print('Error')
if __name__ == '__main__':
google_search('Kevin Rodrigues')
Find more information at http://code.google.com/apis/ajaxsearch/documentation/#fonje
Related Posts:
Tags: programs

