-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes Issue #215 #316
Fixes Issue #215 #316
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ sphinx_settings.json | |
venv/ | ||
env/ | ||
.gitignore | ||
|
||
.venv | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ In this exercise we are going to access Reddit to obtain data like the one in th | |
import json | ||
|
||
# The Reddit URL we'll access | ||
url = "https://cors.bridged.cc/http://www.reddit.com/r/Python/.json" | ||
url = "https://api.allorigins.win/raw?url=http://www.reddit.com/r/Python/.json" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CORS error still exits and lecture not working |
||
resp = urllib.request.urlopen(url).read() | ||
|
||
# The response is in JSON format, it needs to be transformed | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,41 +13,47 @@ La documentación de la API de `TasteDive <https://tastedive.com/read/api>`_. | |
|
||
.. activecode:: ac_l45_3a | ||
:nocodelens: | ||
:language: python | ||
:language: python3 | ||
:python3_interpreter: brython | ||
|
||
En este caso, utilizaremos la librería ``requests`` para hacer la solicitud a la API. La url base | ||
es ``"https://tastedive.com/api/similar"``. A esta url se le va a pasar un parámetro ``q`` con el | ||
valor de la artista Ariana Grande. Al final la url se va a ver de la siguiente forma: ``"https://tastedive.com/api/similar?q=ariana+grande"``. | ||
Note que después de la url base se escribe un ``?`` para indicar que siguen los parámetros. | ||
|
||
~~~~ | ||
import requests | ||
import urllib.request | ||
import urllib.parse | ||
import json | ||
|
||
api_url = "https://tastedive.com/api/similar" | ||
proxy = "https://cors.bridged.cc/" | ||
proxy = "https://api.allorigins.win/raw?url=" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CORS still not working |
||
|
||
# Los parámetros que se pasarán a la url se escriben dentro de un diccionario. | ||
parámetros = {"q": "ariana grande"} | ||
|
||
# Los parámetros que se le pasaran a la url los escribimos dentro de un diccionario | ||
parametros = {"q": "ariana grande"} | ||
#Codificamos los parámetros | ||
parámetros = urllib.parse.urlencode(parámetros) | ||
|
||
# Solicitamos a la api los datos | ||
respuesta = requests.get(proxy + api_url, params=parametros) | ||
# Solicitamos los datos de la api | ||
respuesta = urllib.request.urlopen(proxy + api_url + '?' + parámetros) | ||
|
||
# Ahora imprimimos la url | ||
print(respuesta.url) | ||
print() | ||
# Leemos la respuesta | ||
datos = respuesta.leer() | ||
|
||
# Transformamos los datos de formato json a Python | ||
datos = json.loads(respuesta.text) | ||
# Analizamos los datos JSON | ||
json_data = json.loads(datos) | ||
|
||
print(datos) | ||
# Ahora imprimimos los datos | ||
print(json.dumps(json_data, indent=4)) | ||
|
||
|
||
En el ejemplo anterior pudo apreciar que la API regresa un texto, que si lo pasamos por ``json.loads`` | ||
se transforma a un diccionario de Python. Sin embargo, no es del todo legible. Esto se puede solucionar con | ||
``json.dumps``. | ||
|
||
.. activecode:: ac_l45_3b | ||
:nocodelens: | ||
:language: python3 | ||
:python3_interpreter: brython | ||
|
||
|
@@ -62,7 +68,8 @@ se transforma a un diccionario de Python. Sin embargo, no es del todo legible. E | |
import json | ||
|
||
api_url = "https://tastedive.com/api/similar?" | ||
proxy = "https://cors.bridged.cc/" | ||
proxy = "https://api.allorigins.win/raw?url=" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here too, CORS |
||
|
||
# La siguiente línea es para los parámetros de la url. | ||
parametros = urllib.parse.urlencode({"q": "coldplay"}) | ||
|
||
|
@@ -111,7 +118,8 @@ El siguiente ejercicio viene con calificación automática. | |
import json | ||
|
||
api_url = "https://tastedive.com/api/similar" | ||
proxy = "https://cors.bridged.cc/" | ||
proxy = "https://api.allorigins.win/raw?url=" | ||
|
||
|
||
# Agregue los parámetros | ||
parametros = {} | ||
|
@@ -144,8 +152,8 @@ El siguiente ejercicio viene con calificación automática. | |
def testOne(self): | ||
self.assertEqual( | ||
solicitud_url, | ||
"https://cors.bridged.cc/https://tastedive.com/api/similar?q=Coco&limit=5&info=1", | ||
"Probando que la url sea: https://cors.bridged.cc/https://tastedive.com/api/similar?q=Coco&limit=5&info=1", | ||
"https://api.allorigins.win/raw?url=https://tastedive.com/api/similar?q=Coco&limit=5&info=1", | ||
"Probando que la url sea: https://api.allorigins.win/raw?url=https://tastedive.com/api/similar?q=Coco&limit=5&info=1", | ||
) | ||
self.assertEqual(resultados, 5, "Probando que resultados esté asignado correctamente.") | ||
self.assertEqual(len(peliculas_similares), 5, "Probando que peliculas_similares sean: 5") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CORS error still exists