设为首页收藏本站

Crossin的编程教室

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

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

  [复制链接]

1

主题

0

好友

28

积分

新手上路

Rank: 1

楼主
发表于 2015-10-8 15:52:56 |显示全部楼层
  1. import urllib2
  2. import time
  3. url1 = 'http://m.weather.com.cn/data5/city.xml'
  4. content1 = urllib2.urlopen(url1).read()
  5. provinces = content1.split(',')
  6. result = 'city = {\n'
  7. url = 'http://m.weather.com.cn/data3/city%s.xml'
  8. for p in provinces:
  9.     p_code = p.split('|')[0]
  10.     url2 = url % p_code
  11.     time.sleep(0.5)
  12.     content2 = urllib2.urlopen(url2).read()
  13.     cities = content2.split(',')
  14.     for c in cities:
  15.         c_code = c.split('|')[0]
  16.         url3 = url % c_code
  17.         time.sleep(0.5)
  18.         content3 = urllib2.urlopen(url3).read()
  19.         districts = content3.split(',')
  20.         for d in districts:
  21.             d_pair = d.split('|')
  22.             d_code = d_pair[0]
  23.             name = d_pair[1]
  24.             url4 = url % d_code
  25.             time.sleep(0.5)
  26.             content4 = urllib2.urlopen(url4).read()
  27.             code = content4.split('|')[1]
  28.             line = "    '%s': '%s',\n" % (name, code)
  29.             result += line
  30.             print  name.decode('utf-8') + ':' + code
  31. result += '}'
  32. f = file('d:\city.py', 'w')
  33. f.write(result.encode('utf-8'))
  34. f.close()
  35. print '>>>>>>>>>>DONE!<<<<<<<<<<'
复制代码
基本就是按照教程上来的,加了几个暂停,输出一部分以后就报错了~
北京:101010100
海淀:101010200
朝阳:101010300
顺义:101010400
怀柔:101010500
通州:101010600
昌平:101010700
延庆:101010800
丰台:101010900
石景山:101011000
大兴:101011100
房山:101011200
密云:101011300
门头沟:101011400
平谷:101011500
八达岭:101011600
佛爷顶:101011700
汤河口:101011800
密云上甸子:101011900
斋堂:101012000
霞云岭:101012100
上海:101020100
闵行:101020200
宝山:101020300
嘉定:101020400
南汇:101020500
金山:101020600
青浦:101020800
松江:101020800
奉贤:101020900
崇明:101021000
天津:101030100
武清:101030200
宝坻:101030300
东丽:101030400
西青:101030500
北辰:101030600
宁河:101030700
汉沽:101030800
静海:101030900
津南:101031000
塘沽:101031100
大港:101031200
蓟县:101031300
重庆:101040100
永川:101040200
合川:101040300
南川:101040400
江津:101040500
万盛:101040600
渝北:101040700
北碚:101040800
巴南:101040900
长寿:101041000
黔江:101041100
万州天城:101041200
万州龙宝:101041300
涪陵:101041400

Traceback (most recent call last):
  File "D:/BaiduYunDownload/Python/练习文档/catch.py", line 27, in <module>
    code = content4.split('|')[1]
IndexError: list index out of range
回复

使用道具 举报

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

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

GMT+8, 2024-5-4 16:19 , Processed in 0.015390 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部