GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Home
  • Learn Python
  • Python Tutorial
  • Basics
  • Lists
  • Dictionary
  • Code Snippets
  • Comments
  • Modules
  • API
  • Beautiful Soup
  • Cheatsheet
  • Games
  • Loops

This view

  • Original page
  • New address

GeezerVison is showing www.pythonforbeginners.com — Google Command Line Script - PythonForBeginners.com

You are here: Home / Code Snippets / Google Command Line Script

Overview

Todays post will show how you can make a Google Command Line script with Python (version 2.7.x)

“”” Note: The Google Web Search API has been officially deprecated as of November 1, 2010. It will continue to work as per our deprecation policy, but the number of requests you may make per day will be limited. Therefore, we encourage you to move to the new Custom Search API. “””

To make a request to the Web search API, we have to import the modules that we need.

urllib2
Loads the URL response

urllib
To make use of urlencode

json
Google returns JSON

Next we specify the URL for which we do the request too: http://ajax.googleapis.com/ajax/services/search/web?v=1.0&

To make it a bit interactive, we will ask the user for an input and save the result to a variable that we name “query”.

query = raw_input("What do you want to search for ? >> ")

Create the response object by loading the the URL response, including the query we asked for above.

response = urllib2.urlopen (url + query ).read()

# Process the JSON string. data = json.loads (response)

From this point we can play around with the results

GoogleSearch.py

Let’s see the complete script

import urllib2
import urllib
import json

url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&"

query = raw_input("What do you want to search for ? >> ")

query = urllib.urlencode( {'q' : query } )

response = urllib2.urlopen (url + query ).read()

data = json.loads ( response )

results = data [ 'responseData' ] [ 'results' ]

for result in results:
    title = result['title']
    url = result['url']
    print ( title + '; ' + url )

Open an text editor , copy & paste the code above.

Save the file as GoogleSearch.py and exit the editor.

Run the script: $ python searchGoogle.py

What do you want to search for ? >> python for beginners
BeginnersGuide - Python Wiki; http://wiki.python.org/moin/BeginnersGuide
Python For Beginners; http://www.python.org/about/gettingstarted/
Python For Beginners; https://www.pythonforbeginners.com/

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files formatting Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scripts Split Strings System & OS urllib2
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.