0

I am trying get data of this site http://veiculos.fipe.org.br/

the json file I want to get is the one that appears on the right of the image

url = "http://veiculos.fipe.org.br/api/veiculos/ConsultarValorComTodosParametros"

data = {"codigoTabelaReferencia" : "215",
    "codigoMarca" : "2",
    "codigoModelo" : "4564",
    "codigoTipoVeiculo" : "1",
    "anoModelo" : "2015",
    "codigoTipoCombustivel " : "3",
    "tipoVeiculo" : "carro",
    "modeloCodigoExterno" :  "",
    "tipoConsulta" : "tradicional"
    }

c = requests.post(url,data=data)
c.json()

image

when the last line is executed this error meassege return:

JSONDecodeError: Expecting value: line 2 column 1 (char 2)

At the end of the code I want this dictionary to be returned:

{"Valor":"R$ 14.421,00","Marca":"Alfa Romeo","Modelo":"145 Quadrifoglio 2.0","AnoModelo":1999,"Combustivel":"Gasolina","CodigoFipe":"006002-0","MesReferencia":"agosto de 2018 ","Autenticacao":"hsd0d2ycx5","TipoVeiculo":1,"SiglaCombustivel":"G","DataConsulta":"terça-feira, 28 de agosto de 2018 16:10"}
Rafael
  • 171
  • 1
  • 6
  • Is this what you are looking for? https://stackoverflow.com/questions/16877422/whats-the-best-way-to-parse-a-json-response-from-the-requests-library – Matthew Aug 28 '18 at 19:00
  • yeah, but i run the the same methods of the answer of this link, but i receive a error message – Rafael Aug 28 '18 at 19:07
  • Maybe try it like this instead `import urllib3 http = urllib3.PoolManager() response = http.request('GET', url)` – Matthew Aug 28 '18 at 19:15
  • Your post doesn't return raw json. It returns a webpage. Furthermore the page itself states that, "NÃO disponibilizamos serviço de API." – Ryan Schaefer Aug 28 '18 at 19:25
  • but in the response of the post method there is a dict / json. I want to know if there's a way to get it – Rafael Aug 28 '18 at 19:30

2 Answers2

0
import requests
import json

url = "http://veiculos.fipe.org.br/api/veiculos/ConsultarValorComTodosParametros"

data = {"codigoTabelaReferencia" : "215",
    "codigoMarca" : "2",
    "codigoModelo" : "4564",
    "codigoTipoVeiculo" : "1",
    "anoModelo" : "2015",
    "codigoTipoCombustivel " : "3",
    "tipoVeiculo" : "carro",
    "modeloCodigoExterno" :  "",
    "tipoConsulta" : "tradicional"
    }

c = requests.post(url,data=data)

print json.dumps(data)
bigbounty
  • 13,123
  • 4
  • 20
  • 50
  • Welcome to Stack Overflow! Please indent code with four spaces so it gets rendered properly. The easiest way to do this is to [edit] your answer, select the code, and click the `{}` button or press Ctrl+K. Also, please include some text to explain how your answer works. Stack Overflow is mostly about helping users learn. – Chris Aug 28 '18 at 19:08
  • dont work. Will be printed the the dict that i pass as parameter of the post method, not the return – Rafael Aug 28 '18 at 19:19
0

Try this:

import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0',
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Language': 'en-US,en;q=0.5',
    'Referer': 'http://veiculos.fipe.org.br/',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'X-Requested-With': 'XMLHttpRequest',
    'DNT': '1',
    'Connection': 'keep-alive',
}

data = [
  ('codigoTabelaReferencia', '232'),
  ('codigoMarca', '3'),
  ('codigoModelo', '7'),
  ('codigoTipoVeiculo', '1'),
  ('anoModelo', '1999'),
  ('codigoTipoCombustivel', '1'),
  ('tipoVeiculo', 'carro'),
  ('modeloCodigoExterno', ''),
  ('tipoConsulta', 'tradicional'),
]

response = requests.post('http://veiculos.fipe.org.br/api/veiculos/ConsultarValorComTodosParametros', headers=headers, data=data)

print(response.json())
afonso
  • 7,371
  • 5
  • 23
  • 48