您的位置:首页 > 编程语言 > Python开发

python基础教程共60课-第46课查天气3

2015-12-29 10:30 519 查看
python基础教程共60课-第46课查天气3

明天俺就要出发了,今天赶在睡觉前来个深夜档。

这一课算是“查天气”程序的附加内容。没有这一课,你也查到天气了。但了解一下城市代码的抓取过程,会对网页抓取有更深的理解。

天气网的城市代码信息结构比较复杂,所有代码按层级放在了很多xml为后缀的文件中。而这些所谓的“xml”文件又不符合xml的格式规范,导致在浏览器中无法显示,给我们的抓取又多加了一点难度。

首先,抓取省份的列表:

url1 ='http://m.weather.com.cn/data5/city.xml'

content1 =urllib2.urlopen(url1).read()

provinces =content1.split(',')

输出content1可以查看全部省份代码:

01|北京,02|上海,03|天津,...

对于每个省,抓取城市列表:

url ='http://m.weather.com.cn/data3/city%s.xml'

for p inprovinces:

p_code =p.split('|')[0]

url2 = url %p_code

content2 = urllib2.urlopen(url2).read()

cities =content2.split(',')

输出content2可以查看此省份下所有城市代码:

1901|南京,1902|无锡,1903|镇江,...

再对于每个城市,抓取地区列表:

for c incities[:3]:

c_code =c.split('|')[0]

url3 = url %c_code

content3 =urllib2.urlopen(url3).read()

districts =content3.split(',')

content3是此城市下所有地区代码:

190101|南京,190102|溧水,190103|高淳,...

最后,对于每个地区,我们把它的名字记录下来,然后再发送一次请求,得到它的最终代码:

for d indistricts:

d_pair =d.split('|')

d_code = d_pair[0]

name = d_pair[1]

url4 = url %d_code

content4 =urllib2.urlopen(url4).read()

code = content4.split('|')[1]

name和code就是我们最终要得到的城市代码信息。它们格式化到字符串中,最终保存在文件里:

line = "'%s': '%s',\n" % (name, code)

result += line

同时你也可以输出它们,以便在抓取的过程中查看进度:

print name + ':' +code

完整代码:

import urllib2

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 inprovinces:

p_code =p.split('|')[0]

url2 = url %p_code

content2 =urllib2.urlopen(url2).read()

cities = content2.split(',')

for c in cities:

c_code =c.split('|')[0]

url3 = url %c_code

content3 =urllib2.urlopen(url3).read()

districts =content3.split(',')

for d indistricts:

d_pair =d.split('|')

d_code = d_pair[0]

name = d_pair[1]

url4 = url %d_code

content4 = urllib2.urlopen(url4).read()

code =content4.split('|')[1]

line = "'%s': '%s',\n" % (name, code)

result += line

print name + ':' +code

result += '}'

f =file('/home/crossin/Desktop/city.py', 'w')

f.write(result)

f.close()

如果你只是想抓几个测试一下,并不用全部抓下来,在provices后面加上[:3],抓3个省的试试看就好了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: