0
我在看this codegolf problem,并决定尝试采取python solution并使用urllib
来代替。我修改some sample code与urllib
操纵json
:无法解码堆栈交换API的unicode
import urllib.request
import json
res = urllib.request.urlopen('http://api.stackexchange.com/questions?sort=hot&site=codegolf')
res_body = res.read()
j = json.loads(res_body.decode("utf-8"))
这给:
➜ codegolf python clickbait.py
Traceback (most recent call last):
File "clickbait.py", line 7, in <module>
j = json.loads(res_body.decode("utf-8"))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
如果你去:http://api.stackexchange.com/questions?sort=hot&site=codegolf并点击 “头”,它说charset=utf-8
。为什么它会给我这些奇怪的结果urlopen
?