34

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

sudh
  • 1,085
  • 4
  • 12
  • 13
  • 12
    I'm sure someone can. What have you written so far? – jball Oct 10 '10 at 01:14
  • Or how basic of help are you looking for? [Beginners](http://stackoverflow.com/questions/207701/python-tutorial-for-total-beginners)? Getting started with [web scraping](http://stackoverflow.com/questions/2081586/web-scraping-with-python)? – jball Oct 10 '10 at 01:17
  • @jball i havent coded yet. I am new to python. I have learnt the basic structure of coding in python. So inorder to implement google search can u suggest me where to start with. what kind of modules to use? – sudh Oct 10 '10 at 01:18

8 Answers8

34

Try this, its very simple to use: https://pypi.python.org/pypi/google

Docs: https://breakingcode.wordpress.com/2010/06/29/google-search-python/

Github: https://github.com/MarioVilas/google

Install this python package and usage is as simple as this:

# Get the first 5 hits for "google 1.9.1 python" in Google Pakistan
from google import search

for url in search('google 1.9.1 python', tld='com.pk', lang='es', stop=5):
    print(url)
Mansoor Akram
  • 1,997
  • 4
  • 24
  • 40
  • http://www.geeksforgeeks.org/performing-google-search-using-python-code/ – GorvGoyl Jun 07 '17 at 05:20
  • This worked for me, except that using the tld='...' key consistently resulted in a "connection refused" error. To search for an exact phrase, you can surround it in double quotes, just as you would for a normal google search. –  Apr 08 '19 at 16:27
25

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 = result['url']   # was URL in the original and that threw a name error exception
    print ( title + '; ' + url )

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

[Edit] As the AJAX API is dead, you can use a third party service, like SerpApi, they do provide a Python library.

Hartator
  • 5,029
  • 4
  • 43
  • 73
LK-
  • 282
  • 3
  • 2
1

As @Zloy Smiertniy pointed out, the answer can be found here.

However, if you are using Python 3 the syntax of raw_input, urllib has changed, and one has to decode the response. Thus, for Python 3 one can use:

import urllib
import urllib.request
import json
url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
query = input("Query:")
query = urllib.parse.urlencode( {'q' : query } )
response = urllib.request.urlopen (url + query ).read()
data = json.loads ( response.decode() )
results = data [ 'responseData' ] [ 'results' ]
for result in results:
    title = result['title']
    url = result['url']
    print ( title + '; ' + url )
Løiten
  • 3,185
  • 4
  • 24
  • 36
  • In python3 i am getting this error: 'module' object has no attribute 'parse' – Mansoor Akram Jun 29 '16 at 08:04
  • Hmm...strange. It should be there according to https://docs.python.org/3/library/urllib.html. I noticed that after rerunning the script that I had to add `import urllib.request` (now in the updated answer), so maybe `import urllib.parse` could help in your case. Also note that after rerunning the script, I had troubles getting an output for `response`, so the `url` may have changed since I used the script last time. Best of luck – Løiten Jun 29 '16 at 09:33
  • This API is no longer available, the alternative is [google-custom-search](https://developers.google.com/custom-search/). – ands Aug 28 '17 at 00:32
1

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

Shiv Deepak
  • 3,122
  • 5
  • 34
  • 49
0

Try the following:

import webbrowser
lib = input()
url = "https://www.google.co.in/search?q=" +(str(lib))+ "&oq="+(str(lib))+"&gs_l=serp.12..0i71l8.0.0.0.6391.0.0.0.0.0.0.0.0..0.0....0...1c..64.serp..0.0.0.UiQhpfaBsuU"
webbrowser.open_new(url)
Johan Lundberg
  • 26,184
  • 12
  • 71
  • 97
mayank
  • 305
  • 3
  • 3
  • This doesn't answer the OP's question. It just opens the page in a browser window (even though OP asked for python script) and it doesn't print out the results (links). – ands Aug 28 '17 at 00:38
-3

I'm a newbie to Python. Just my simple idea for a google search.

import webbrowser
lib=raw_input("Enter what you want to search for:")
ur="https://www.google.co.in/gfe_rd=cr&ei=Q7nZVqSBIMSL8QeBpbOoDQ#q="
webbrowser.open_new(ur+lib)
  • 2
    This doesn't actually do what was asked; it performs a google search by opening a browser window, but the Python code never receives the search results to display (or otherwise manipulate). – Mogsdad Mar 05 '16 at 17:42
-3

I've used SERP API to accomplish this.

The instructions are fairly simple:

pip install google-search-results

and the usage is:

from lib.google_search_results import GoogleSearchResults
query = GoogleSearchResults({"q": "coffee"})
json_results = query.get_json()

More advanced uses are on Github.

lf2225
  • 405
  • 4
  • 6
-7
from pygoogle import pygoogle
g = pygoogle('quake 3 arena')
g.pages = 5
print '*Found %s results*'%(g.get_result_count())
g.get_urls()
  • 9
    Dude you just copied and pasted that from the pygoogle, which no longer works fyi. You provided zero explain as well. – reticentroot Sep 28 '15 at 03:30