- 帖子
- 2
- 精华
- 0
- 积分
- 8
- 阅读权限
- 10
- 注册时间
- 2017-11-20
- 最后登录
- 2017-11-23
|
# -*- coding: utf-8 -*-
from urllib.request import urlopen #打开网页函数
import urllib
import json
from city import city #把city文件作为函数引用
f=open('c:\Python34\city.py')
cityname=input('你想查哪个城市的天气?\n')
citycode=city.get(cityname)
if citycode:
url=("http://www.weather.com.cn/weather1d/%s.shtml#search" %citycode)
content=urlopen(url).read().decode() #解码网址
data=json.loads(content,'utf-8') #把字符串转成字典
result=data["weatherinfo"]
str_temp=("%s\n%s~%s")%(result["weather"],result["temp1"],result["temp2"])
print(str_temp)
f.close()
不管有没有decode都是在json.loads那里出错,请大神指教 |
|