python请求无法解码utf-8API响应

python请求无法解码utf-8API响应,python,json,python-3.x,encoding,python-requests,Python,Json,Python 3.x,Encoding,Python Requests,我正在尝试从以下API端点获取JSON响应https://datos.madrid.es/egob/catalogo/205026-0-cementerios.json。我的代码是: import requests url = 'https://datos.madrid.es/egob/catalogo/205026-0-cementerios.json' r = requests.get(url) r.json() 它失败并出现以下错误: json.decoder.JSONDecodeEr

我正在尝试从以下API端点获取JSON响应
https://datos.madrid.es/egob/catalogo/205026-0-cementerios.json
。我的代码是:

import requests

url = 'https://datos.madrid.es/egob/catalogo/205026-0-cementerios.json'
r = requests.get(url)
r.json()
它失败并出现以下错误:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
如果我从请求中获取编码,则它是空的。因此,我尝试在接受之前强制编码,但没有成功:

import requests

url = 'https://datos.madrid.es/egob/catalogo/205026-0-cementerios.json'
r = requests.get(url)
r.encoding = 'utf-8'
r.json()
给出了相同的错误

r.text
返回如下内容:

'\x00\x00\x01\x00\x01\x00  \x00\x00\x01\x00\x18\x0 .......
看来它没有正确地解码响应


我如何才能成功解码它?

它似乎已压缩。解压缩它,然后使用
json.decode
。编码是
utf-8

例如:

import zlib
decompressed_data=zlib.decompress(f.read(), 16+zlib.MAX_WBITS)
你的URL是公共的,你可以用你最喜欢的浏览器测试它。Chrome提供以下标题:

Cache-Control: no-cache
Connection: Keep-Alive
Content-disposition: inline;filename=205026-0-cementerios.json
Content-Encoding: gzip
Content-Length: 4383
Content-Type: application/json;charset=UTF-8
Date: Thu, 20 Dec 2018 12:19:33 GMT
OT-force-Account-Verify: true
Vary: Accept-Encoding
X-Frame-Options: SAMEORIGIN
X-UA-Compatible: IE=8
Xonnection: close
解压后,它看起来很好
json

{
"@context": {
    "c": "http://www.w3.org/2002/12/cal#",
    "dcterms": "http://purl.org/dc/terms/",
    "geo": "http://www.w3.org/2003/01/geo/wgs84_pos#",
    "loc": "http://purl.org/ctic/infraestructuras/localizacion#",
    "org": "http://purl.org/ctic/infraestructuras/organizacion#",
    "vcard": "http://www.w3.org/2006/vcard/ns#",
    "title": "vcard:fn",
    "id": "dcterms:identifier",
    "relation": "dcterms:relation",
    "references": "dcterms:references",
    "address": "vcard:adr",
    "area": "loc:barrio",
    "district": "loc:distrito",
    "locality": "vcard:locality",
    "postal-code": "vcard:postal-code",
    "street": "vcard:street-address",
    "location": "vcard:geo",
    "latitude": "geo:lat",
    "longitude": "geo:long",
....

服务器在用户代理头上做了一些奇怪的事情(即如果无法识别favicon,则返回favicon!)。您可以通过强制用户代理解决此问题:

url = 'https://datos.madrid.es/egob/catalogo/205026-0-cementerios.json'
r = requests.get(url, headers={"User-Agent": "curl/7.61.0"})
print(r.json())

非常好用,谢谢!我不知道favicon的事!谢谢我将另一个答案标记为接受的答案,只是因为它需要更少的代码,但是你的答案也可以,所以我对它进行了升级