- 帖子
- 2
- 精华
- 0
- 积分
- 16
- 阅读权限
- 10
- 注册时间
- 2016-9-9
- 最后登录
- 2016-9-22
|
本帖最后由 sunbaodi 于 2016-9-16 23:47 编辑
开始遇到无法读取city.py的问题,在city.py里面加了coding:utf-8,但是后面输入城市之后就自动结束程序了,后来看其他同学的回复琢磨出正确的方法了,谢谢crossin先生,谢谢各位。
介绍下情况,win7系统,编辑器:notepad++。
代码如下:
# -*- coding:utf-8 -*-
import urllib2
import json
from city import city
cityname = raw_input('你想查哪里城市的天气?\n'.decode('utf-8').encode('gbk'))
citycode = city.get(cityname.decode('gbk').encode('utf-8'))
if citycode:
url = ('http://www.weather.com.cn/data/cityinfo/%s.html' % citycode)
content = urllib2.urlopen(url).read().decode('utf-8').encode('gbk')
print content
运行情况:
PS C:\Users\sunbaodi\mystuff> python 024.py
你想查哪里城市的天气?
无锡
{"weatherinfo":{"city":"无锡","cityid":"101190201","temp1":"4℃","temp2":"16℃",
"weather":"晴转多云","img1":"n0.gif","img2":"d1.gif","ptime":"18:00"}} |
|