这一次我们将要采集与分析的是国家地震台网的数据,原网页为http://data.earthquake.cn/datashare/globeEarthquake_csn.html。仍然使用python来自动化采集数据,使用R来进行数据分析。我们需要提取原网页中table中的地震数据。
其网页源文件中数据显示的格式如下,使用re模块的正则表达式来提取数据,需要注意标题行和数据行格式的不同,另外数据行中“时间”这一列是不规范的,设计匹配规则时为了一次性抓取数据,需要较强的兼容规则。
数据保存在csv文件中,python数据采集源代码:
<span style="font-size:14px;">#encoding:utf-8
'''
Created on 2015年1月31日
@author: Guo
'''
import urllib2,re,csv
url='http://data.earthquake.cn/datashare/globeEarthquake_csn.html'
page=urllib2.urlopen(url).read().decode('gb2312').encode('utf-8')#原网页编码方式为gb2312需要先解码后编码为平台可显示的utf-8字符格式
pattern=re.compile('<