0

Estoy intentando acceder a la lista de la key "data" que se encuentra en un diccionario dentro de otro diccionario.

El diccionario es el siguiente:

{"message":{"type":"Buffer","data":[137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,70,0,0,0,40,8,6,0,0,0,67,208,140,174,0,0,0,6,98,75,71,68,0,255,0,255,0,255,160,189,167,147,0,0,6,195,73,68,65,84,104,67,237,217,117,168,45,85,20,199,241,107,39,182,162,152,216,137,138,138,216,45,34,6,152,127,152,216,221,173,224,195,238,70,81,81,175,98,119,163,216,141,221,221,221,221,237,239,115,223,140,30,231,157,186,231,121,67,56,11,190,204,185,51,179,102,239,189,246,218,107,173,189,111,79,79,87,186,22,232,90,160,107,129,225,107,129,241,134,111,215,134,174,103,227,166,233,151,194,60,67,215,133,81,90,30,63,119,166,8,179,134,5,194,226,97,229,176,78,216,56,108,19,246,9,35,194,49,225,204,176,231,64,244,127,235,124,244,137,192,72,253,149,137,163,48,77,49,136,133,115,93,42,172,18,214,15,155,135,29,194,190,225,240,112,98,49,136,203,115,189,49,220,17,30,11,38,230,157,240,69,248,61,252,84,252,126,61,215,103,195,195,225,182,112,117,184,176,248,198,209,185,30,28,24,136,161,86,234,111,199,219,125,255,222,188,120,75,216,47,28,21,78,13,231,4,131,112,223,115,131,208,217,15,195,215,225,207,240,109,248,184,184,207,184,15,20,131,160,215,27,78,47,190,119,96,174,187,23,131,216,32,215,53,194,138,97,145,48,87,152,41,76,30,198,106,183,195,131,245,222,122,105,232,187,112,65,48,195,59,135,45,131,153,95,45,44,91,12,98,182,92,167,11,147,14,86,199,134,67,59,107,167,19,175,5,203,163,43,21,11,244,230,111,238,255,127,144,78,98,98,199,227,154,36,154,111,134,213,59,254,194,192,43,206,151,38,46,9,2,248,160,202,210,105,237,221,48,229,160,182,218,186,49,6,17,3,63,10,35,194,144,196,185,227,210,176,244,56,28,100,254,138,65,120,245,144,137,106,248,233,176,209,144,245,160,167,167,214,32,50,229,132,67,216,151,127,53,189,80,254,250,36,168,49,6,83,84,187,234,32,75,230,63,55,136,146,90,109,178,87,216,35,40,167,219,73,195,85,61,149,230,93,97,140,22,150,225,222,107,5,165,185,193,108,30,212,60,173,100,236,188,176,124,216,49,156,20,20,137,42,224,3,194,4,45,148,107,117,181,233,27,42,224,186,217,202,205,99,195,151,65,193,166,58,253,37,188,17,62,11,74,232,122,131,108,166,71,255,186,6,122,42,85,165,185,10,88,91,248,42,60,21,62,8,87,4,198,174,138,62,236,20,84,209,159,6,91,1,186,191,134,247,130,106,90,145,89,175,175,181,186,218,176,133,160,235,42,105,140,162,107,243,101,118,237,65,236,63,30,15,140,116,90,209,73,17,253,190,96,211,85,43,51,231,143,31,194,251,5,6,85,171,39,8,51,142,65,86,197,44,151,157,186,38,191,109,234,212,65,111,133,51,130,231,250,83,59,64,191,123,131,118,236,145,24,197,126,72,192,63,47,88,190,210,177,237,197,249,13,116,61,187,172,120,215,183,108,99,234,233,246,245,247,148,226,131,55,228,106,195,86,219,25,169,87,48,221,62,60,19,54,236,211,24,41,244,110,14,47,132,183,3,79,179,47,210,152,37,232,125,75,138,241,106,131,241,10,249,219,4,208,181,17,173,21,203,192,247,150,11,38,200,198,178,20,70,183,92,120,181,25,158,161,162,59,117,209,62,239,126,40,216,136,150,98,185,184,183,119,241,93,239,214,74,85,183,103,218,60,181,84,150,8,102,193,250,171,202,162,185,97,224,214,161,93,43,169,167,103,80,182,249,12,195,64,223,135,31,131,194,79,27,165,136,95,60,196,78,215,32,170,114,124,110,136,111,12,225,93,125,187,41,252,22,120,135,111,205,81,71,207,173,57,3,207,89,44,88,90,150,58,120,181,113,88,126,237,232,246,108,146,23,47,13,118,174,174,141,132,65,52,102,144,115,135,118,244,28,43,48,54,207,224,246,126,91,142,135,133,151,195,115,193,70,179,42,231,230,198,102,225,254,192,107,222,10,118,237,150,141,247,45,251,102,114,79,30,174,26,30,9,138,78,222,199,91,250,163,219,179,127,20,142,8,162,179,153,110,36,58,107,215,124,113,112,232,211,137,30,47,112,6,34,165,242,38,71,17,218,228,101,101,70,152,62,191,121,4,195,9,168,60,70,109,36,160,90,186,187,5,241,167,153,120,111,215,32,102,89,78,157,232,246,53,116,114,216,37,152,149,70,114,100,30,56,115,225,202,82,107,167,122,229,247,203,101,39,6,113,249,159,195,231,193,114,249,35,168,69,148,242,165,56,151,225,105,7,133,67,155,154,229,159,247,28,114,73,223,157,232,246,44,25,197,114,153,188,146,223,99,54,104,180,236,84,105,160,78,245,170,159,31,145,27,101,186,101,148,50,117,243,40,217,204,185,142,180,93,206,58,79,48,145,205,196,4,155,104,30,35,105,116,162,219,151,129,30,13,91,5,199,128,130,98,181,6,224,222,214,55,203,59,157,219,162,120,167,19,189,218,1,89,255,79,6,89,198,105,159,182,175,13,188,135,23,125,19,196,19,153,200,210,114,207,146,23,123,154,137,231,18,133,62,47,19,100,65,105,90,134,115,162,216,142,110,223,59,142,8,21,60,35,130,122,69,122,62,33,232,168,2,77,102,49,147,235,22,31,118,180,56,58,122,12,114,103,16,99,196,156,106,38,180,132,100,15,177,76,54,225,49,2,39,47,122,62,88,106,244,121,66,181,8,156,55,247,44,67,231,200,138,192,113,130,58,77,241,102,15,37,99,73,30,245,164,86,247,239,231,179,231,23,163,188,26,100,11,133,89,233,214,10,42,129,80,135,205,98,109,201,221,31,61,179,200,3,12,110,211,208,236,156,246,162,60,151,249,174,15,50,38,145,218,21,125,226,134,138,220,59,138,204,82,100,65,91,3,203,136,215,48,92,41,165,174,165,168,148,152,170,230,153,159,85,221,202,227,145,135,204,74,110,46,171,248,90,48,56,183,21,135,100,37,69,96,61,105,166,167,46,177,92,24,156,65,32,118,52,147,50,150,201,90,250,66,196,191,171,194,237,129,81,204,126,111,240,142,234,220,223,130,185,9,246,188,54,36,244,87,183,69,247,70,62,22,123,88,159,55,89,6,237,74,89,131,136,25,130,55,15,177,206,95,12,140,100,179,218,72,212,59,2,103,121,45,223,243,13,179,207,8,98,78,185,204,85,215,234,36,203,200,127,21,234,237,149,70,71,119,148,126,114,75,174,39,210,95,217,166,69,212,37,42,101,157,101,84,179,92,138,186,68,69,107,150,235,85,189,222,115,126,162,228,23,39,124,199,218,175,138,218,70,172,83,234,243,40,94,190,124,16,79,90,201,232,232,246,89,92,65,166,99,10,42,129,210,255,111,26,137,247,215,12,50,149,64,174,72,147,86,171,122,254,47,164,34,101,100,129,183,42,142,56,108,62,21,148,54,177,181,70,109,53,224,1,127,238,191,130,162,190,32,233,224,216,117,198,6,173,150,6,225,85,254,133,98,137,52,211,179,214,203,45,130,128,126,107,176,113,229,69,118,189,178,199,221,65,250,126,48,76,54,224,163,109,209,128,92,111,187,111,80,42,80,87,51,127,118,131,206,149,6,81,20,242,14,174,223,142,158,110,112,229,67,138,247,203,204,167,184,19,160,165,105,237,170,178,7,245,223,28,141,236,179,109,30,148,21,168,3,163,222,96,211,88,21,51,110,201,72,139,210,248,89,109,234,213,107,119,162,220,20,143,100,63,113,98,187,160,162,86,123,12,27,153,37,61,17,192,108,241,235,29,13,150,6,81,169,218,169,50,14,175,105,165,55,108,6,56,16,29,113,200,36,197,42,171,109,231,187,82,88,64,96,228,242,93,233,90,160,107,129,186,22,248,11,30,227,9,57,252,255,206,12,0,0,0,0,73,69,78,68,174,66,96,130]}}

y el script que estoy realizando es el siguiente:

#!/usr/bin/python
#coding: utf-8
import requests

url  = "https://apicovid19.kairosmining.com/auth/new_Captcha"

def makeRequest():
    s = requests.session()
    response = s.post(url)
    diccionario = response.text
    print(diccionario["data"])

if __name__ == '__main__':
    makeRequest()

sin embargo, me arroja el siguiente error al intentar imprimirlo:

introducir la descripción de la imagen aquí

El objetivo final es poder convertir esa data en una imagen.

1
  • Si no estas seguro del tipo de dato que tie es puedes hacer print(type(diccionario)). Por lógico se sabe que tu variable diccionario es un string
    – Christian
    Commented el 23 jul. 2021 a las 0:40

2 respuestas 2

1

Sin tantos rodeos, el problema es que la respuesta lo recibe como texto y lo ideas es recibirlo como un json. Así que cambiamos a esto:

def makeRequest():
    ...
    diccionario = response.json

Una ves tiendo esto, la respuesta ya se asemeja a lo que muestras (como tipo de dato), ahora como la clave data se encuentra dentro de message, primero tendremos que acceder a esa clave

def makeRequest():
    s = requests.session()
    response = s.post(url)
    diccionario = response.json
    print(diccionario["message"]["data"])
1

El código que ponés es incorrecto porque, además de no acceder correctamente al campo del diccionario que te interesa, estás intentando acceder a un string como si fuera un diccionario. La variable diccionario no contiene un diccionario, sino un string que devuelve response.text.

Para usarlo adecuadamente tenés que convertir el string en un diccionario, lo cuál podés hacer como sigue:

#import json
diccionario = json.loads(response.txt)

Luego, para acceder correctamente al campo del diccionario que te interesa, deberías hacerlo así (replicando tu código):

print(diccionario["message"]["data"])

El código finalmente te quedaría así:

#!/usr/bin/python
#coding: utf-8
import requests, json

url  = "https://apicovid19.kairosmining.com/auth/new_Captcha"

def makeRequest():
    s = requests.session()
    response = s.post(url)
    cadena = response.text
    diccionario = json.loads(cadena)
    print(diccionario["message"]["data"])

if __name__ == '__main__':
    makeRequest()

Espero que te sirva, ¡saludos!

5
  • Al intentar de acceder de esa forma al diccionario, me encuentro con el siguiente problema: print(diccionario["message"]["data"]) TypeError: string indices must be integers Intenté con anterioridad esa forma, sin embargo, tampoco resultó.
    – Gohanckz
    Commented el 22 jul. 2021 a las 23:53
  • Pues es la forma en que deberías acceder al campo value de la key data del diccionario que pusiste en la pregunta. El error que comentás suena a que intentás acceder a un iterable tipo lista, tupla o string (que usan índice entero). Estás seguro que en la variable diccionario tenés ese diccionario?
    – Juan Pablo
    Commented el 23 jul. 2021 a las 0:06
  • Sep seguro, la respuesta que da el post en texto es la de un diccionario, este lo almacené en la variable y solo lo imprimí mostrando el diccionario que dejo en la descripción de la pregunta, sin embargo me interesa acceder solo a la lista del diccionario en particular lo cual no he podido realizar a causa de estos errores.
    – Gohanckz
    Commented el 23 jul. 2021 a las 0:37
  • 1
    Fijate en la respuesta, te puse un anexo. La variable diccionario no contiene un diccionario sino un string. Si votaste negativo, por favor cambialo, porque además de ser correcta mi respuesta, estás confundiendo a la comunidad.
    – Juan Pablo
    Commented el 23 jul. 2021 a las 0:38
  • 1
    Solución: has response.json y luego haces diccionario["message"]["data"]
    – Christian
    Commented el 23 jul. 2021 a las 0:43

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.