數(shù)據(jù)來(lái)源:北京地鐵官網(wǎng)
我是一只小爬蟲(chóng)否过,咿呀咿呀呦
技術(shù):python
很早以前就想做一只小小爬蟲(chóng)参萄,奈何一直沒(méi)那個(gè)時(shí)間與心思,今天在昨天學(xué)習(xí)的基礎(chǔ)上惩系,更進(jìn)一步,用python做了一只簡(jiǎn)陋的小爬蟲(chóng)如筛,爬取了北京地鐵官網(wǎng)上每日公布的日客流量數(shù)據(jù)堡牡,做了一份報(bào)表
1、Python小爬蟲(chóng)部分
1杨刨、1 學(xué)習(xí)知識(shí)
- Python基礎(chǔ)知識(shí)
- Python中urllib和urllib2庫(kù)的用法
- Python正則表達(dá)式
- Python爬蟲(chóng)框架Scrapy
- Python爬蟲(chóng)更高級(jí)的功能
由于本爬蟲(chóng)實(shí)現(xiàn)及邏輯簡(jiǎn)單因此并未使用到后面的框架晤柄,登錄等部分
1、2 urllib部分
#encoding:UTF-8
import urllib.request
url = "http://www.baidu.com"
data = urllib.request.urlopen(url).read()
data = data.decode('UTF-8')
print(data)
1妖胀、3 爬取北京地鐵官網(wǎng)數(shù)據(jù)
-
主要使用urllib模塊
-
爬取頁(yè)面分析
url如下:
http://www.bjsubway.com/support/cxyd/klxx/index_10.html
并且每一個(gè)部分都一樣可免,只是數(shù)字10改變,從2到147個(gè)頁(yè)面
-
需要數(shù)據(jù)分析
數(shù)據(jù)在一個(gè)P標(biāo)簽內(nèi)做粤,因此可以使用re.compile('<p>(.+?)</p>')
正則簡(jiǎn)單匹配
1、4 前期爬取工作代碼
import re
import urllib.request
import urllib
import xlsxwriter as xls
def GetOnePage(url):
res=[]
urlop=urllib.request.urlopen(url)
data=urlop.read()
data = data.decode('GBK')
linkre = re.compile('<p>(.+?)</p>')
for x in linkre.findall(data):
res.append(x)
return res
data=[]
url='http://www.bjsubway.com/support/cxyd/klxx/index.html'
data+=GetOnePage(url)
for i in range(2,148):
#for i in range(2,3):
url='http://www.bjsubway.com/support/cxyd/klxx/index_'+str(i)+'.html'
#print(url)
data+=GetOnePage(url)
workbook = xls.Workbook('北京地鐵原始數(shù)據(jù).xlsx')
worksheet = workbook.add_worksheet() #()中可以加入名字
worksheet.write_column('A1',data)
workbook.close()
本來(lái)想一步到位捉撮,從爬去到保存成圖怕品,但是北京地鐵官網(wǎng)對(duì)多次發(fā)送的IP會(huì)進(jìn)行攔截。人家有看門(mén)狗狗巾遭。囧~~~
其實(shí)最后我的數(shù)據(jù)也沒(méi)有拉全肉康,可能在中間請(qǐng)求的過(guò)程中已被攔截闯估。沒(méi)那么多時(shí)間管這個(gè),就將就用了拉取的數(shù)據(jù)吼和。
2涨薪、數(shù)據(jù)分析部分
2.1 原始數(shù)據(jù)分析
- 正確爬取的數(shù)據(jù)
5月25日(周三),北京地鐵公司所轄15條運(yùn)營(yíng)線路日客運(yùn)量為923.73萬(wàn)人次(不含京港地鐵所轄:4號(hào)線炫乓、14號(hào)線刚夺、大興線客流)。各條線路日客運(yùn)量詳見(jiàn)下圖
首先可以用
split(' , ')
截取時(shí)間末捣,之后線路一定是在條
前的侠姑,客運(yùn)量一定在為 XXX萬(wàn)
之間的
當(dāng)然原本我是這么自以為是的,因此出現(xiàn)了各種問(wèn)題
- 正確爬取的另類數(shù)據(jù)
4月29日(周五)箩做,北京地鐵公司所轄15條運(yùn)營(yíng)線路日客運(yùn)量再創(chuàng)新高莽红,達(dá)到1050.26萬(wàn)人次(不含京港地鐵所轄:4號(hào)線、14號(hào)線邦邦、大興線客流)安吁。各條線路日客
在這條數(shù)據(jù)中,日客運(yùn)量數(shù)據(jù)是在
達(dá)到XXX萬(wàn)人次
- 錯(cuò)誤爬取的數(shù)據(jù)
</p><div class="card_pic_img"><a id="example7" href="/d/file/support/cxyd/klxx/2015-10-08/b0fe2bf2e38e66343ca43197af90b8a1.jpg"
由于正則的簡(jiǎn)單燃辖,難免會(huì)爬取一些錯(cuò)誤的數(shù)據(jù)
2.2 將數(shù)據(jù)進(jìn)行處理
-
經(jīng)過(guò)之前的分析鬼店,現(xiàn)將每一條數(shù)據(jù),截取出日期郭赐,管轄線路條數(shù)薪韩,日客運(yùn)量三列數(shù)據(jù)
-
代碼
import xlrd
import os
import xlsxwriter as xls
#讀取xlxs文件,將車站保存到內(nèi)存
filename=r'{0}\北京地鐵原始數(shù)據(jù).xlsx'.format(os.getcwd())
data = xlrd.open_workbook(filename)
sheetname = data.sheet_names()
sheet = data.sheet_by_index(0)
rows = sheet.nrows
cols = sheet.ncols
time=[]
lines=[]
renshu=[]
for row in range(rows):
temp=sheet.row_values(row)[0].split('捌锭,')
inX=temp[1].index('條')
inW=0
if '為' in temp[1]:
inW=temp[1].index('為')
elif '達(dá)到' in temp[1]:
intW =temp[1].index('達(dá)到')
else:
continue
inWan=temp[1].index('萬(wàn)')
time.append(temp[0])
lines.append(temp[1][inX-2:inX])
renshu.append(float(temp[1][inW+1:inWan]))
time=time[::-1]
lines=lines[::-1]
renshu=renshu[::-1]
workbook=xls.Workbook('北京地鐵運(yùn)營(yíng)公司全年日客運(yùn)量.xlsx')
worksheet=workbook.add_worksheet('北京地鐵運(yùn)營(yíng)公司')
bold=workbook.add_format({'bold':1})
#添加表頭
headings=['時(shí)間','線路','日客運(yùn)量']
worksheet.write_row('A1',headings,bold)
#添加內(nèi)容
worksheet.write_column('A2',time)
worksheet.write_column('B2',lines)
worksheet.write_column('C2',renshu)
#創(chuàng)建新的圖表俘陷,折線圖
chart=workbook.add_chart({'type':'line'})
chart.add_series({
'name': '={0}!$B$1'.format('北京地鐵運(yùn)營(yíng)公司'),
'categories': '={0}!$A$2:$A${1}'.format('北京地鐵運(yùn)營(yíng)公司',len(time)+1),
'values': '={0}!$C$2:$C${1}'.format('北京地鐵運(yùn)營(yíng)公司',len(time)+1),
})
#添加表頭和xy
chart.set_title({'name':'北京地鐵運(yùn)營(yíng)公司全年日客運(yùn)量變化趨勢(shì)'})
chart.set_x_axis({'name':'時(shí)間'})
chart.set_y_axis({'name':'人數(shù)(萬(wàn)人)'})
#Set an Excel chart style. Colors with white outline and shadow.
chart.set_style(10)
# Insert the chart into the worksheet (with an offset).
worksheet.insert_chart('E2', chart, {'x_offset': 25, 'y_offset': 10})
workbook.close()