Could anyone help me on how to write a python script that searches google and prints the links of top results.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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)
Method 2
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.
Method 3
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
Method 4
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 )
Method 5
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)
Method 6
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)
Method 7
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.
Method 8
from pygoogle import pygoogle
g = pygoogle('quake 3 arena')
g.pages = 5
print '*Found %s results*'%(g.get_result_count())
g.get_urls()
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0