我正在使用python 3.4并尝试从URL解析看似有效的JSON输出。 例如:http://api.stackexchange.com/2.2/questions?order=desc&sort=activity&site=stackoverflow
这是我的代码的样子
import json
from urllib.request import urlopen
def jsonify(url):
response = urlopen(url).read().decode('utf8')
repo = json.loads(response)
return repo
url = jsonify('http://api.stackexchange.com/2.2/questions?order=desc&sort=activity&site=stackoverflow');
但是,我收到诸如UnicodeDecodeError utf-8 codec can't decode byte 0x8b in position 1; invalid start byte
该脚本适用于任何其他API,如github和许多其他API,但不适用于stackexchange api。
使用 gzip
压缩响应,您必须解压缩它。
$ curl -v http://api.stackexchange.com/2.2/questions?order=desc&sort=activity&site=stackoverflow
* Trying 198.252.206.16...
* TCP_NODELAY set
* Connected to api.stackexchange.com (198.252.206.16) port 80 (#0)
> GET /2.2/questions?order=desc&sort=activity&site=stackoverflow HTTP/1.1
> Host: api.stackexchange.com
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Cache-Control: private
< Content-Type: application/json; charset=utf-8
< Content-Encoding: gzip
有关更多详细信息,请参阅 api.stackexchange 文档。
减压示例:
import gzip
def jsonify(url):
response = urlopen(url).read()
tmp = gzip.decompress(response).decode('utf-8')
repo = json.loads(tmp)
return repo