Python 如何解决JSONDECODE错误?

Python 如何解决JSONDECODE错误?,python,json,python-requests,Python,Json,Python Requests,我正在使用requests Python库应用requests.post()并将一些信息保存为响应对象。看起来一切正常,但我无法将响应转换为json()文件 我已经尝试将内容解码为“utf-8”,但似乎无法解决问题。我还尝试使用re模块提取请求响应内容中的JSON # importing the requests library import requests import json # api-endpoint URL='http://www.aguacanal.es/regantes

我正在使用requests Python库应用requests.post()并将一些信息保存为响应对象。看起来一切正常,但我无法将响应转换为json()文件

我已经尝试将内容解码为“utf-8”,但似乎无法解决问题。我还尝试使用re模块提取请求响应内容中的JSON

# importing the requests library 
import requests 
import json

# api-endpoint 
URL='http://www.aguacanal.es/regantes/'

# defining a params dict for the parameters to be sent to the API 
PARAMS = {"idsector":"5","arqueta":"961","tipocontador":"21","fechaini":"04/04/2019","fechafin":"04/04/2019 23:00",}

# sending get request and saving the response as response object 
r = requests.post(url = URL, params = PARAMS,stream=True) 

print(r.status_code)  

r.json()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
r、 json()
json格式的文件“C:\Users\sara.iglesias\AppData\Local\Continuum\anaconda3\lib\site packages\requests\models.py”,第896行
返回complexjson.load(self.text,**kwargs)
文件“C:\Users\sara.iglesias\AppData\Local\Continuum\anaconda3\lib\json\\uuuuu init\uuuuu.py”,第348行,加载
返回\u默认\u解码器。解码
文件“C:\Users\sara.iglesias\AppData\Local\Continuum\anaconda3\lib\json\decoder.py”,第337行,在decode中
obj,end=self.raw\u decode(s,idx=\u w(s,0.end())
文件“C:\Users\sara.iglesias\AppData\Local\Continuum\anaconda3\lib\json\decoder.py”,第355行,原始解码
从None引发JSONDecodeError(“预期值”,s,err.value)
JSONDecodeError:应为值

您的代码返回的是html而不是json

r.content
Out[10]: b'<!DOCTYPE html>\n<!--[if IE 7]>\n<html class="ie ie7">\n<![endif]-->\n<!--[if IE 8]>\n<html class="ie ie8">\n<![endif]-->\n<!--[if !(IE 7) | !(IE 8)]-->\n<html lang="es-ES" prefix="og: http://ogp.me/ns#" xmlns:og="https://ogp.me/ns#" xmlns:fb="https://www.facebook.com/2008/fbml">\n<!--<![endif]-->\n<head>\n    <meta charset="UTF-8" />\n    <meta name="viewport" content="width=device-width" />\n ...
r.content
Out[10]:b'\n\n\n\n\n\n\n\n\n。。。

明显的问题:该请求返回什么…?显然它不是JSON,它返回requests.models.Response,但通过函数JSON(),它应该转换为JSON文件。
r.content
Out[10]: b'<!DOCTYPE html>\n<!--[if IE 7]>\n<html class="ie ie7">\n<![endif]-->\n<!--[if IE 8]>\n<html class="ie ie8">\n<![endif]-->\n<!--[if !(IE 7) | !(IE 8)]-->\n<html lang="es-ES" prefix="og: http://ogp.me/ns#" xmlns:og="https://ogp.me/ns#" xmlns:fb="https://www.facebook.com/2008/fbml">\n<!--<![endif]-->\n<head>\n    <meta charset="UTF-8" />\n    <meta name="viewport" content="width=device-width" />\n ...