Created
June 27, 2012 12:51
-
-
Save zoek1/3003898 to your computer and use it in GitHub Desktop.
@CodeJobs GoogleSearch #Python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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: ")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actualización a python 3 :)