tags:

views:

128

answers:

4

Could anyone help me on how to write a python script that searches google and prints the links of top results.

+2  A: 

Did you try using Google to look for "Google search API"? I think it provides what you need.

Jim Garrison
http://dcortesi.com/2008/05/28/google-ajax-search-api-example-python-code/
jball
+1  A: 

A few suggestions:

1) Play around with google search via the url: http://www.google.com/#hl=en&q=stack+overflow

2) Look into python's urllib http://docs.python.org/library/urllib.html

3) Familiarize yourself with html - specifically the <a> tag.

That should be enough to get you started.

Edit:

After reading some of the other responses, I looked into Google Search APIs. The Developer's Guide was very helpful, though the most of the article deals with JavaScript. For this reason, you may want to skip directly to Flash and other Non-Javascript Environments. The code snippets in that section have an example dealing specifically with Python.

I'd also recommend reading the Class Reference - specifically Flash and other Non-Javascript Environments, which offers more useful information for the problem you are trying to solve.

RevolXadda
A: 

Maybe, something like this?

import urllib
import json as m_json
query = raw_input ( 'Query: ' )
query = urllib.urlencode ( { 'q' : query } )
response = urllib.urlopen ( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query ).read()
json = m_json.loads ( response )
results = json [ 'responseData' ] [ 'results' ]
for result in results:
    title = result['title']
    url = url['url']
    print ( title + '; ' + url )

Read the docs http://docs.python.org/

LK-
+1  A: 

it is better suggested to use google apis but a very ugly version.. (alternative to use google api) you can filter content if you want

import os, urllib, sys
filename = 'http://www.google.com/search?' + urllib.urlencode({'q': ' '.join(sys.argv[1:]) })
cmd = os.popen("lynx -dump %s" % filename)
output = cmd.read()
cmd.close()
print output

it will print exactly what ever a browser should display when you search for something on google

Idlecool
interesting for the "lynx -dump"
mt3