Home > Blockchain >  Can't extract simple information from an api
Can't extract simple information from an api

Time:12-31

I'm trying to get information out of an api. If I understand correctly, once I use the json module I should get a dictionary where I should be able to extract the information from.

But no matter what key I use I get an error

import requests


r = requests.get('https://api.mercadolibre.com/sites/MLA/search?q=gt 710  usado &sort=price_asc')
a  = r.json()

print(a['title'])

Following error no matter what keyword I use

Traceback (most recent call last):
  File "C:\Users\asdf\Desktop\la\some.py", line 8, in <module>
    a["title"]
KeyError: 'title'

CodePudding user response:

title is nested under results in your json object.

import requests

r = requests.get('https://api.mercadolibre.com/sites/MLA/search?q=gt 710  usado &sort=price_asc')
a = r.json()

for result in a['results']:
    print(result['title'])

Output

Gt 710 (1gb)  
Placa De Video Msi Geforce Gt 710 1gb
Placa De Video Nvidia Galax  Geforce 700 Series Gt 7102gb
Placa De Video Nvidia Gigabyte Geforce 700 Series Gt 710 2gb
Placa De Video Asus  Geforce 700 Series Gt 710-sl-2gd5 2gb
Placa De Video Nvidia Geforce Gt 740
Placa De Video Evga 710 1gb Ddr3 
Gtx 770 4gb Zotac 
Placa De Video Msi Geforce Gt 710 2gd3 Lp 2gb
Placa De Video Nvidia Msi  Geforce 700 Series Gt 710 Gt 710 
Placa De Video Nvidia Msi  Geforce 700 Series Gt 710 Gt 710 
Placa De Vídeo Nvidia Gt710
Placa De Video Msi Geforce Gt 710 1gb
Placa De Video Msigt710 
Placa De Video Nvidia Msi Gt 710
Asus Gt 710 
Placa De Vídeo Nvidia 
Placa De Video Nvidia Msi Gt 710 Gt 710 1gd3h Lp 1gb - Nueva
Placa De Video Msi Geforce Gt 710 1gb
Placa De Video Colorful Geforce Gt710 1gb Ddr3 Hdmi
Tarjeta Grafica
Asus Geforce Low Profile Gt 710 1gb Gddr5   Cable Vga
Placa De Video Gt 710 2gb Ddr3 Gainward 3 Meses De Uso
Placa De Video Nvidia Geforce Gt 710 1gb Msi Casi Nueva
Placa De Video Nvidia Asus Geforce Gt 710 1gb
Placa De Video Gt 710 2gb Ddr3
Placa De Video Evga Gt 710 2 Gb Ddr3
Placa De Video Nvidia Msi Geforce Gt 710 1gd3h Lp 1gb
Placa De Video Msi Nvidia Gt 710 2gb Low Profile
Placa De Video Nvidia Msi  Geforce Gt 710 1gb Gddr3
Placa De Video Nvidia Gt 710 Low Profile
Nvidia Gt 710 2gb Pny
Placa De Video Gt710 1gb Gddr5
Gt 710 2gb
Nvidia Msi Gt 710 2gb
Nvidia Geforce Gt 710 2gb Ddr3
Placa De Video Nvidia Geforce Gt 710
Placa De Video Nvidia Asus  Geforce Gt 710 2gb Ddr5
Placa De Video Gt 710 2gb Galax
Placa De Video Nvidia Asus  Geforce 700 Series Gt 710 2gb
Tarjeta Grafica Geforce 710 2gb Nvidia
Msi Nvidia Gt 710 2gb Gddr3
Gigabyte Geforce Gt 710 2gb Ddr3 
Geforce Gt 710 2gb Asus 
Nvidia Msi Gt 710 2gb Ddr3 Low Profile. Nueva 3 Meses De Uso
Placa De Video Msi Gt 710 2gb
Placa De Video Nvidia Msi  Geforce 700 Series Gt 7102gb Ddr3
Placa De Video Gefroce Evga Gtx 750
  • Related