设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 143660|回复: 1
打印 上一主题 下一主题

【Python 第44课】 查天气(2)

  [复制链接]

0

主题

0

好友

36

积分

新手上路

Rank: 1

楼主
发表于 2013-7-24 09:03:54 |显示全部楼层

回帖奖励 +1

  1. import urllib.request
  2. from city import city

  3. exit=False

  4. while not exit:
  5.     cityname=input("你想查询哪个城市的天气?输入q退出\n")
  6.     if cityname=="q" or cityname=="Q":
  7.         print("退出!")
  8.         exit=True
  9.     else:
  10.         citycode=city.get(cityname)
  11.         if citycode:
  12.             url=("http://www.weather.com.cn/data/cityinfo/%s.html"%citycode)
  13.             request=urllib.request.Request(url)
  14.             response=urllib.request.urlopen(request)
  15.             content=response.read().decode("utf-8")
  16.             print(content)
复制代码
回复

使用道具 举报

0

主题

0

好友

36

积分

新手上路

Rank: 1

沙发
发表于 2013-7-24 09:07:47 |显示全部楼层
feicien 发表于 2013-7-24 09:03

我下载的是python3.3
发现语法与python2.7有很大区别,这是我在python3.3下写的代码
加了一个while循环,下面是运行的结果:
  1. Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32
  2. Type "copyright", "credits" or "license()" for more information.
  3. >>> ================================ RESTART ================================
  4. >>>
  5. 你想查询哪个城市的天气?输入q退出
  6. 北京
  7. {"weatherinfo":{"city":"北京","cityid":"101010100","temp1":"36℃","temp2":"23℃","weather":"晴","img1":"d0.gif","img2":"n0.gif","ptime":"08:00"}}
  8. 你想查询哪个城市的天气?输入q退出
  9. 天津
  10. {"weatherinfo":{"city":"天津","cityid":"101030100","temp1":"36℃","temp2":"26℃","weather":"晴转多云","img1":"d0.gif","img2":"n1.gif","ptime":"08:00"}}
  11. 你想查询哪个城市的天气?输入q退出
  12. 上海、
  13. 你想查询哪个城市的天气?输入q退出
  14. 上海
  15. {"weatherinfo":{"city":"上海","cityid":"101020100","temp1":"39℃","temp2":"30℃","weather":"晴转多云","img1":"d0.gif","img2":"n1.gif","ptime":"08:00"}}
  16. 你想查询哪个城市的天气?输入q退出
  17. 杭州
  18. {"weatherinfo":{"city":"杭州","cityid":"101210101","temp1":"39℃","temp2":"29℃","weather":"晴","img1":"d0.gif","img2":"n0.gif","ptime":"08:00"}}
  19. 你想查询哪个城市的天气?输入q退出
  20. Q
  21. 退出!
  22. >>>
复制代码
回复

使用道具 举报

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

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

GMT+8, 2024-5-7 18:35 , Processed in 0.024636 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部