Google JSON API Custom Search Engine for Pywombat
Listar los primeros Pywombat Exercises utilizando la API JSON de Custom Search Engine de Google
En este notebook utilizaremos la API de Google para búsquedas personalizadas sobre un sitio de interés, en este caso Pywombat.
Requerimientos
- requests : Realizar peticiones API JSON de Google
- pandas : Mostrar resultados en forma de Tabla
- nbval : Realizar Test (py.test) sobre notebook
import json
import requests
import pandas as pd
API_KEY="AIzaSyD6MLBUV45-lqHIOlfQ7xuGlkEaYMB7_P4"
URL_API=f"https://www.googleapis.com/customsearch/v1?q=exercise&cx=000045984045285095660:7xaecdr9bat&key={API_KEY}"
results=dict()
columns=('htmlTitle', 'link', 'htmlSnippet')
def getResults():
data=dict()
try:
response=requests.get(URL_API)
data=response.json()
if 'error' in data:
raise Exception(f"{data['error']['message']}")
except Exception as error:
#print(f"ERROR {error} in API {URL_API}")
print(f"ERROR in API {URL_API}")
return data
def loadExercise():
with open('pywombat.json', 'r') as f:
data=json.load(f)
return data['items']
data=getResults()
for idx, item in enumerate(data.get('items',[]), 1):
title=item['htmlTitle']
link=item['link']
snippet=item['htmlSnippet']
results[idx]={'htmlTitle':title,
'link':link,
'htmlSnippet':snippet}
df=pd.DataFrame(data=results, index=columns).T
df
context="Pywombat Exercises"
assert ("context" in data and
context == data['context']['title']),"Error in Google Custom Search API"
print("Google Search API working!")