设为首页收藏本站

Crossin的编程教室

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

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

  [复制链接]

0

主题

1

好友

273

积分

中级会员

Rank: 3Rank: 3

楼主
发表于 2016-1-21 23:43:17 |显示全部楼层
crossin先生,我直接在浏览器输入带有北京code的查天气网址,得到一个有乱码的页面内容,运行程序也报错,是什么问题呢?网址应该是存在的呀~
  1. # -*- coding: cp936 -*-
  2. import urllib2
  3. import json
  4. from city import city
  5. cityname=raw_input("Which city's weather do you want to know?\n")
  6. citycode=city.get(cityname)
  7. print citycode
  8. if citycode:
  9.     url='http://www.weather.com.cn/data/cityinfo/%s.html'%citycode
  10.     content=urllib2.urlopen(url).read()
  11.    
复制代码

Python22.png (14.34 KB, 下载次数: 271)

Python22.png

Python23.png (29.96 KB, 下载次数: 275)

Python23.png

回复

使用道具 举报

0

主题

1

好友

273

积分

中级会员

Rank: 3Rank: 3

沙发
发表于 2016-1-22 16:27:59 |显示全部楼层
crossin先生 发表于 2016-1-22 12:32
404,说明没有请求到页面
在程序中把请求的网址print出来看对不对

今天按照您说的试了一下,citycode返回正确,网址print出来没有问题,也没有出现昨天的问题,但是没有返回任何天气内容,直接就结束了,为什么呢?
  1. # -*- coding: cp936 -*-
  2. import urllib2
  3. import json
  4. from city import city
  5. cityname=raw_input("Which city's weather do you want to know?\n")
  6. citycode=city.get(cityname)
  7. print citycode
  8. if citycode:
  9.     url='http://www.weather.com.cn/data/cityinfo/%s.html'%citycode
  10.     print url
  11.     content=urllib2.urlopen(url).read()
复制代码

Python24.png (6.38 KB, 下载次数: 271)

Python24.png

回复

使用道具 举报

0

主题

1

好友

273

积分

中级会员

Rank: 3Rank: 3

板凳
发表于 2016-1-22 16:46:59 |显示全部楼层
catherinemic 发表于 2016-1-22 16:27
今天按照您说的试了一下,citycode返回正确,网址print出来没有问题,也没有出现昨天的问题,但是没有返回 ...

哎呀,傻了,最后忘记print了。。。这次没问题了~~谢谢crossin先生!
回复

使用道具 举报

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

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

GMT+8, 2024-5-21 10:10 , Processed in 0.020260 second(s), 26 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部