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个省的试试看就好了。
明天俺就要出发了,今天赶在睡觉前来个深夜档。
这一课算是“查天气”程序的附加内容。没有这一课,你也查到天气了。但了解一下城市代码的抓取过程,会对网页抓取有更深的理解。
天气网的城市代码信息结构比较复杂,所有代码按层级放在了很多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个省的试试看就好了。
相关文章推荐
- python基础教程共60课-第45课查天气3
- python基础教程共60课-第44课查天气2
- python3.5.1对应的pywin32的安装问题 import win32api出错
- 1. Two Sum Leetcode Python New season for 2016
- 值得关注的 10 个 Python 英文博客
- Python使用django开发需要使用的包大全
- python第三方库solrpy
- python知识-定制类
- Python发送邮件
- python实现文本文件合并
- Python标准库06 子进程 (subprocess包)
- 从Python 官方文档找知识点答案的方法
- 浅谈Python时间模块
- mac下pydev+eclipse+python环境搭建
- 自学Python之变量
- python实现文本文件合并
- 网狐棋牌游戏 模拟登录封包python实现
- python的函数装饰器学习笔记
- Python 源码的考古(二) 读 0.9.1 源码
- Python 源码的考古(三) 读 0.9.1 源码2