Skip to content

Instantly share code, notes, and snippets.

@zoek1
Created June 27, 2012 12:51
Show Gist options
  • Save zoek1/3003898 to your computer and use it in GitHub Desktop.
Save zoek1/3003898 to your computer and use it in GitHub Desktop.
@CodeJobs GoogleSearch #Python3
#!/usr/bin/python3
import json
import urllib.parse
import urllib.request
def showsome (searchfor):
query = urllib.parse.urlencode ({'q': searchfor})
print (query)
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query
search_response = urllib.request.urlopen (url)
# metodo read devuelve objeto tipo byte
search_results = search_response.read()
search_results = str(search_results)
search_results = search_results.replace("\'","")
search_results = search_results.replace("b","",1)
search_results = search_results.replace('\\','\\\\')
# Entrada de json es una cadena
results = json.loads(search_results)
data = results['responseData']
print('Total results: ' + data['cursor']['estimatedResultCount'])
hits = data['results']
print ('Top %d hits: ' % len(hits))
for h in hits:
print (' ', h['url'])
print('For more results, see ' + data['cursor']['moreResultsUrl'])
showsome(input("Introduce la busqueda: "))
@zoek1
Copy link
Author

zoek1 commented Jun 27, 2012

Actualización a python 3 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment