- 帖子
- 5
- 精华
- 0
- 积分
- 28
- 阅读权限
- 10
- 注册时间
- 2015-10-8
- 最后登录
- 2015-10-30
|
- import urllib2
- import time
- url1 = 'http://m.weather.com.cn/data5/city.xml'
- content1 = urllib2.urlopen(url1).read()
- provinces = content1.split(',')
- result = 'city = {\n'
- url = 'http://m.weather.com.cn/data3/city%s.xml'
- for p in provinces:
- p_code = p.split('|')[0]
- url2 = url % p_code
- time.sleep(0.5)
- content2 = urllib2.urlopen(url2).read()
- cities = content2.split(',')
- for c in cities:
- c_code = c.split('|')[0]
- url3 = url % c_code
- time.sleep(0.5)
- content3 = urllib2.urlopen(url3).read()
- districts = content3.split(',')
- for d in districts:
- d_pair = d.split('|')
- d_code = d_pair[0]
- name = d_pair[1]
- url4 = url % d_code
- time.sleep(0.5)
- content4 = urllib2.urlopen(url4).read()
- code = content4.split('|')[1]
- line = " '%s': '%s',\n" % (name, code)
- result += line
- print name.decode('utf-8') + ':' + code
- result += '}'
- f = file('d:\city.py', 'w')
- f.write(result.encode('utf-8'))
- f.close()
- 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
|
|