[英]python requests fails to decode utf-8 API response
我正在尝试从以下 API 端点https://datos.madrid.es/egob/catalogo/205026-0-cementerios.json
获取响应 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)
您的网址是公开的,您可以使用自己喜欢的浏览器进行测试。 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!)。 您可以通过强制用户代理解决此问题:
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())
如果您使用标头并具有"Accept-Encoding": "gzip, deflate, br"
请使用 pip install 安装 brotli 库。 您不需要将 brotli 导入您的 py 文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.