报错的原因
html = response.read()
print html
# [Output]:
# jQuery214041126974549961415_1554559702413({"status":"1","info":"OK","infocode":"10000","datas":[{"id":"70037","name":"矿院","aqi":"144","pm2.5":"102","so2":"12","no2":"18","o3":"204","co":"900","pm10":"239"}]})
print type(html)
这里的html变量的类型是str,且不能被json解析。主要原因是'jQuery214041126974549961415_1554559702413('这部分不能用json解析。
解决办法
利用re模块根据'('或')'分割上述html字符串获得一个list,提取其中第1个元素(从0开始计数)即为所需的json可解析部分。注意在程序开头import re
import re # 添加到程序开始处!
# 下面两行code可以合并
html = re.split('[()]', html)
html = html[1]
print html
# [Output]: