设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

【Python 第45课】 查天气(3)

[复制链接]

0

主题

0

好友

8

积分

新手上路

Rank: 1

楼主
发表于 2017-11-23 08:26:26 |显示全部楼层
# -*- 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那里出错,请大神指教
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-21 03:50 , Processed in 0.016256 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部