设为首页收藏本站

Crossin的编程教室

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

【Python 第46课】 查天气(4)

  [复制链接]

0

主题

1

好友

132

积分

注册会员

Rank: 2

楼主
发表于 2013-9-2 17:23:00 |显示全部楼层
在不用[:3]减少查询城市的时候,始终会报一个错误:
  code = content4.split('|')[1]
IndexError: list index out of range

用了[:3]后,则可以正确写入数据,不知道是为什么。
回复

使用道具 举报

0

主题

1

好友

132

积分

注册会员

Rank: 2

沙发
发表于 2013-9-2 20:24:36 |显示全部楼层
crossin先生 发表于 2013-9-2 18:51
很可能是,查询多了,对方服务器有时候没有回复成功。
你把contect4打出来看看,错的时候是什么数据 ...

试了5次,有3次是放回一张html文件的源代码,应该是长404页面,

<div class="main">
  <h1>很抱歉,您要访问的页面不存在。</h1>
    <p>请直接点击下面的连接进入中国天气网首页:<a href="http://www.weather.com.cn/">www.weather.com.cn</a></p>
<div class="ewm">
        <h3><a href="http://3g.weather.com.cn/">中国天气网客户端</a></h3>
        <dl>
          <dt><a href="http://3g.weather.com.cn/"><img src="/poster/chart.png"></a></dt>
          <dd><a href="http://3g.weather.com.cn/">点击或扫描下载</a></dd>
        </dl>

      </div>
</div>


有2次是返回下面的报错:

Traceback (most recent call last):
  File "/Users/aresli/Sites/Dropbox/file/python/lesson46.py", line 33, in <module>
    content4 = urllib2.urlopen(url4).read()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 400, in open
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 513, in http_response
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 438, in error
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 372, in _call_chain
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 521, in http_error_default
urllib2.HTTPError: HTTP Error 404: Not Found


是不是网络问题或者是中国天气网的数据问题?
回复

使用道具 举报

0

主题

1

好友

132

积分

注册会员

Rank: 2

板凳
发表于 2013-9-2 22:46:38 |显示全部楼层
crossin先生 发表于 2013-9-2 20:37
最近经常这样,请求多了它那里就拿不到数据。在循环里面sleep一下,可能会好些。 ...

在第二层和第三层循环中分别加了 time.sleep(0.1),结果还是一样。

不纠结了,掌握程序的思路就可以了。
回复

使用道具 举报

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

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

GMT+8, 2024-5-3 00:05 , Processed in 0.026515 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部