设为首页收藏本站

Crossin的编程教室

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

【Python 第43课】 查天气(1)

[复制链接]

0

主题

0

好友

6

积分

新手上路

Rank: 1

楼主
发表于 2018-8-10 10:21:52 |显示全部楼层
查询结果出错了 好像是输入的“北京”编码有错?url识别不了?
但我不懂怎么改。。。

#coding:cp936
import urllib2
import gzip
from StringIO import StringIO

cityname=raw_input('你想查哪个城市的天气?\n')
url='http://wthrcdn.etouch.cn/weather_mini?city=%s'%cityname
content=urllib2.urlopen(url).read() #读取天气信息

#gzip解压
buf = StringIO(content)
f = gzip.GzipFile(fileobj=buf)
content = f.read()

print content


======== RESTART: C:\Users\Administrator\Desktop\003_WeatherReport.py ========
你想查哪个城市的天气?
北京
{"status":1002,"desc":"invilad-citykey"}
>>>
回复

使用道具 举报

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

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

GMT+8, 2024-5-3 23:34 , Processed in 0.026032 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部