- 帖子
- 1
- 精华
- 0
- 积分
- 7
- 阅读权限
- 10
- 注册时间
- 2018-7-19
- 最后登录
- 2018-7-20
|
代码:
import urllib.request
import json
from city import city
cityname = input('city name\n')
citycode=city.get(cityname)
if citycode:
url = ('http://www.weather.com.cn/data/cityinfo/%s.html' % citycode)
content = urllib.request.urlopen(url).read()
print(content)
print (json.dumps(json.loads(content), ensure_ascii=False) )
结果如下:
b'{"weatherinfo":{"city":"\xe5\x8c\x97\xe4\xba\xac","cityid":"101010100","temp1":"-2\xe2\x84\x83","temp2":"16\xe2\x84\x83","weather":"\xe6\x99\xb4","img1":"n0.gif","img2":"d0.gif","ptime":"18:00"}}'
{"weatherinfo": {"city": "北京", "cityid": "101010100", "temp1": "-2℃", "temp2": "16℃", "weather": "晴", "img1": "n0.gif", "img2": "d0.gif", "ptime": "18:00"}}
按照教程里的做法中文无法显示,下边的结果是百度出来的一种方法,如何像教学里的一样可以直接显示中文呢?
|
|