
Python實現(xiàn)多線程抓取網(wǎng)頁功能實例詳解
本文實例講述了Python實現(xiàn)多線程抓取網(wǎng)頁功能。分享給大家供大家參考,具體如下:
最近,一直在做網(wǎng)絡(luò)爬蟲相關(guān)的東西。 看了一下開源C++寫的larbin爬蟲,仔細(xì)閱讀了里面的設(shè)計思想和一些關(guān)鍵技術(shù)的實現(xiàn)。
1、larbin的URL去重用的很高效的bloom filter算法;
2、DNS處理,使用的adns異步的開源組件;
3、對于url隊列的處理,則是用部分緩存到內(nèi)存,部分寫入文件的策略。
4、larbin對文件的相關(guān)操作做了很多工作
5、在larbin里有連接池,通過創(chuàng)建套接字,向目標(biāo)站點發(fā)送HTTP協(xié)議中GET方法,獲取內(nèi)容,再解析header之類的東西
6、大量描述字,通過poll方法進(jìn)行I/O復(fù)用,很高效
7、larbin可配置性很強(qiáng)
8、作者所使用的大量數(shù)據(jù)結(jié)構(gòu)都是自己從最底層寫起的,基本沒用STL之類的東西
......
還有很多,以后有時間在好好寫篇文章,總結(jié)下。
這兩天,用python寫了個多線程下載頁面的程序,對于I/O密集的應(yīng)用而言,多線程顯然是個很好的解決方案。剛剛寫過的線程池,也正好可以利用上了。其實用python爬取頁面非常簡單,有個urllib2的模塊,使用起來很方便,基本兩三行代碼就可以搞定。雖然使用第三方模塊,可以很方便的解決問題,但是對個人的技術(shù)積累而言沒有什么好處,因為關(guān)鍵的算法都是別人實現(xiàn)的,而不是你自己實現(xiàn)的,很多細(xì)節(jié)的東西,你根本就無法了解。 我們做技術(shù)的,不能一味的只是用別人寫好的模塊或是api,要自己動手實現(xiàn),才能讓自己學(xué)習(xí)得更多。
我決定從socket寫起,也是去封裝GET協(xié)議,解析header,而且還可以把DNS的解析過程單獨處理,例如DNS緩存一下,所以這樣自己寫的話,可控性更強(qiáng),更有利于擴(kuò)展。對于timeout的處理,我用的全局的5秒鐘的超時處理,對于重定位(301or302)的處理是,最多重定位3次,因為之前測試過程中,發(fā)現(xiàn)很多站點的重定位又定位到自己,這樣就無限循環(huán)了,所以設(shè)置了上限。具體原理,比較簡單,直接看代碼就好了。
自己寫完之后,與urllib2進(jìn)行了下性能對比,自己寫的效率還是比較高的,而且urllib2的錯誤率稍高一些,不知道為什么。網(wǎng)上有人說urllib2在多線程背景下有些小問題,具體我也不是特別清楚。
先貼代碼:
fetchPage.py 使用Http協(xié)議的Get方法,進(jìn)行頁面下載,并存儲為文件
'''
Created on 2012-3-13
Get Page using GET method
Default using HTTP Protocol , http port 80
@author: xiaojay
'''
import socket
import statistics
import datetime
import threading
socket.setdefaulttimeout(statistics.timeout)
class Error404(Exception):
'''Can not find the page.'''
pass
class ErrorOther(Exception):
'''Some other exception'''
def __init__(self,code):
#print 'Code :',code
pass
class ErrorTryTooManyTimes(Exception):
'''try too many times'''
pass
def downPage(hostname ,filename , trytimes=0):
try :
#To avoid too many tries .Try times can not be more than max_try_times
if trytimes >= statistics.max_try_times :
raise ErrorTryTooManyTimes
except ErrorTryTooManyTimes :
return statistics.RESULTTRYTOOMANY,hostname+filename
try:
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#DNS cache
if statistics.DNSCache.has_key(hostname):
addr = statistics.DNSCache[hostname]
else:
addr = socket.gethostbyname(hostname)
statistics.DNSCache[hostname] = addr
#connect to http server ,default port 80
s.connect((addr,80))
msg = 'GET '+filename+' HTTP/1.0\r\n'
msg += 'Host: '+hostname+'\r\n'
msg += 'User-Agent:xiaojay\r\n\r\n'
code = ''
f = None
s.sendall(msg)
first = True
while True:
msg = s.recv(40960)
if not len(msg):
if f!=None:
f.flush()
f.close()
break
# Head information must be in the first recv buffer
if first:
first = False
headpos = msg.index("\r\n\r\n")
code,other = dealwithHead(msg[:headpos])
if code=='200':
#statistics.fetched_url += 1
f = open('pages/'+str(abs(hash(hostname+filename))),'w')
f.writelines(msg[headpos+4:])
elif code=='301' or code=='302':
#if code is 301 or 302 , try down again using redirect location
if other.startswith("http") :
hname, fname = parse(other)
downPage(hname,fname,trytimes+1)#try again
else :
downPage(hostname,other,trytimes+1)
elif code=='404':
raise Error404
else :
raise ErrorOther(code)
else:
if f!=None :f.writelines(msg)
s.shutdown(socket.SHUT_RDWR)
s.close()
return statistics.RESULTFETCHED,hostname+filename
except Error404 :
return statistics.RESULTCANNOTFIND,hostname+filename
except ErrorOther:
return statistics.RESULTOTHER,hostname+filename
except socket.timeout:
return statistics.RESULTTIMEOUT,hostname+filename
except Exception, e:
return statistics.RESULTOTHER,hostname+filename
def dealwithHead(head):
'''deal with HTTP HEAD'''
lines = head.splitlines()
fstline = lines[0]
code =fstline.split()[1]
if code == '404' : return (code,None)
if code == '200' : return (code,None)
if code == '301' or code == '302' :
for line in lines[1:]:
p = line.index(':')
key = line[:p]
if key=='Location' :
return (code,line[p+2:])
return (code,None)
def parse(url):
'''Parse a url to hostname+filename'''
try:
u = url.strip().strip('\n').strip('\r').strip('\t')
if u.startswith('http://') :
u = u[7:]
elif u.startswith('https://'):
u = u[8:]
if u.find(':80')>0 :
p = u.index(':80')
p2 = p + 3
else:
if u.find('/')>0:
p = u.index('/')
p2 = p
else:
p = len(u)
p2 = -1
hostname = u[:p]
if p2>0 :
filename = u[p2:]
else : filename = '/'
return hostname, filename
except Exception ,e:
print "Parse wrong : " , url
print e
def PrintDNSCache():
'''print DNS dict'''
n = 1
for hostname in statistics.DNSCache.keys():
print n,'\t',hostname, '\t',statistics.DNSCache[hostname]
n+=1
def dealwithResult(res,url):
'''Deal with the result of downPage'''
statistics.total_url+=1
if res==statistics.RESULTFETCHED :
statistics.fetched_url+=1
print statistics.total_url , '\t fetched :', url
if res==statistics.RESULTCANNOTFIND :
statistics.failed_url+=1
print "Error 404 at : ", url
if res==statistics.RESULTOTHER :
statistics.other_url +=1
print "Error Undefined at : ", url
if res==statistics.RESULTTIMEOUT :
statistics.timeout_url +=1
print "Timeout ",url
if res==statistics.RESULTTRYTOOMANY:
statistics.trytoomany_url+=1
print e ,"Try too many times at", url
if __name__=='__main__':
print 'Get Page using GET method'
下面,我將利用上一篇的線程池作為輔助,實現(xiàn)多線程下的并行爬取,并用上面自己寫的下載頁面的方法和urllib2進(jìn)行一下性能對比。
'''
Created on 2012-3-16
@author: xiaojay
'''
import fetchPage
import threadpool
import datetime
import statistics
import urllib2
'''one thread'''
def usingOneThread(limit):
urlset = open("input.txt","r")
start = datetime.datetime.now()
for u in urlset:
if limit <= 0 : break
limit-=1
hostname , filename = parse(u)
res= fetchPage.downPage(hostname,filename,0)
fetchPage.dealwithResult(res)
end = datetime.datetime.now()
print "Start at :\t" , start
print "End at :\t" , end
print "Total Cost :\t" , end - start
print 'Total fetched :', statistics.fetched_url
'''threadpoll and GET method'''
def callbackfunc(request,result):
fetchPage.dealwithResult(result[0],result[1])
def usingThreadpool(limit,num_thread):
urlset = open("input.txt","r")
start = datetime.datetime.now()
main = threadpool.ThreadPool(num_thread)
for url in urlset :
try :
hostname , filename = fetchPage.parse(url)
req = threadpool.WorkRequest(fetchPage.downPage,args=[hostname,filename],kwds={},callback=callbackfunc)
main.putRequest(req)
except Exception:
print Exception.message
while True:
try:
main.poll()
if statistics.total_url >= limit : break
except threadpool.NoResultsPending:
print "no pending results"
break
except Exception ,e:
print e
end = datetime.datetime.now()
print "Start at :\t" , start
print "End at :\t" , end
print "Total Cost :\t" , end - start
print 'Total url :',statistics.total_url
print 'Total fetched :', statistics.fetched_url
print 'Lost url :', statistics.total_url - statistics.fetched_url
print 'Error 404 :' ,statistics.failed_url
print 'Error timeout :',statistics.timeout_url
print 'Error Try too many times ' ,statistics.trytoomany_url
print 'Error Other faults ',statistics.other_url
main.stop()
'''threadpool and urllib2 '''
def downPageUsingUrlib2(url):
try:
req = urllib2.Request(url)
fd = urllib2.urlopen(req)
f = open("pages3/"+str(abs(hash(url))),'w')
f.write(fd.read())
f.flush()
f.close()
return url ,'success'
except Exception:
return url , None
def writeFile(request,result):
statistics.total_url += 1
if result[1]!=None :
statistics.fetched_url += 1
print statistics.total_url,'\tfetched :', result[0],
else:
statistics.failed_url += 1
print statistics.total_url,'\tLost :',result[0],
def usingThreadpoolUrllib2(limit,num_thread):
urlset = open("input.txt","r")
start = datetime.datetime.now()
main = threadpool.ThreadPool(num_thread)
for url in urlset :
try :
req = threadpool.WorkRequest(downPageUsingUrlib2,args=[url],kwds={},callback=writeFile)
main.putRequest(req)
except Exception ,e:
print e
while True:
try:
main.poll()
if statistics.total_url >= limit : break
except threadpool.NoResultsPending:
print "no pending results"
break
except Exception ,e:
print e
end = datetime.datetime.now()
print "Start at :\t" , start
print "End at :\t" , end
print "Total Cost :\t" , end - start
print 'Total url :',statistics.total_url
print 'Total fetched :', statistics.fetched_url
print 'Lost url :', statistics.total_url - statistics.fetched_url
main.stop()
if __name__ =='__main__':
'''too slow'''
#usingOneThread(100)
'''use Get method'''
#usingThreadpool(3000,50)
'''use urllib2'''
usingThreadpoolUrllib2(3000,50)
實驗分析:
實驗數(shù)據(jù):larbin抓取下來的3000條url,經(jīng)過Mercator隊列模型(我用c++實現(xiàn)的,以后有機(jī)會發(fā)個blog)處理后的url集合,具有隨機(jī)和代表性。使用50個線程的線程池。
實驗環(huán)境:ubuntu10.04,網(wǎng)絡(luò)較好,python2.6
存儲:小文件,每個頁面,一個文件進(jìn)行存儲
PS:由于學(xué)校上網(wǎng)是按流量收費的,做網(wǎng)絡(luò)爬蟲,灰常費流量?。。。∵^幾天,可能會做個大規(guī)模url下載的實驗,用個幾十萬的url試試。
實驗結(jié)果:
使用urllib2,usingThreadpoolUrllib2(3000,50)
Start at : 2012-03-16 22:18:20.956054
End at : 2012-03-16 22:22:15.203018
Total Cost : 0:03:54.246964
Total url : 3001
Total fetched : 2442
Lost url : 559
下載頁面的物理存儲大?。?4088kb
使用自己的getPageUsingGet ,usingThreadpool(3000,50)
Start at : 2012-03-16 22:23:40.206730
End at : 2012-03-16 22:26:26.843563
Total Cost : 0:02:46.636833
Total url : 3002
Total fetched : 2484
Lost url : 518
Error 404 : 94
Error timeout : 312
Error Try too many times 0
Error Other faults 112
下載頁面的物理存儲大?。?7168kb
小結(jié):自己寫的下載頁面程序,效率還是很不錯的,而且丟失的頁面也較少。但其實自己考慮一下,還是有很多地方可以優(yōu)化的,比如文件過于分散,過多的小文件創(chuàng)建和釋放定會產(chǎn)生不小的性能開銷,而且程序里用的是hash命名,也會產(chǎn)生很多的計算,如果有好的策略,其實這些開銷都是可以省略的。另外DNS,也可以不使用python自帶的DNS解析,因為默認(rèn)的DNS解析都是同步的操作,而DNS解析一般比較耗時,可以采取多線程的異步的方式進(jìn)行,再加以適當(dāng)?shù)腄NS緩存很大程度上可以提高效率。不僅如此,在實際的頁面抓取過程中,會有大量的url ,不可能一次性把它們存入內(nèi)存,而應(yīng)該按照一定的策略或是算法進(jìn)行合理的分配。 總之,采集頁面要做的東西以及可以優(yōu)化的東西,還有很多很多。
數(shù)據(jù)分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
MySQL 大表拆分與關(guān)聯(lián)查詢效率:打破 “拆分必慢” 的認(rèn)知誤區(qū) 在 MySQL 數(shù)據(jù)庫管理中,“大表” 始終是性能優(yōu)化繞不開的話題。 ...
2025-09-18CDA 數(shù)據(jù)分析師:表結(jié)構(gòu)數(shù)據(jù) “獲取 - 加工 - 使用” 全流程的賦能者 表結(jié)構(gòu)數(shù)據(jù)(如數(shù)據(jù)庫表、Excel 表、CSV 文件)是企業(yè)數(shù)字 ...
2025-09-18DSGE 模型中的 Et:理性預(yù)期算子的內(nèi)涵、作用與應(yīng)用解析 動態(tài)隨機(jī)一般均衡(Dynamic Stochastic General Equilibrium, DSGE)模 ...
2025-09-17Python 提取 TIF 中地名的完整指南 一、先明確:TIF 中的地名有哪兩種存在形式? 在開始提取前,需先判斷 TIF 文件的類型 —— ...
2025-09-17CDA 數(shù)據(jù)分析師:解鎖表結(jié)構(gòu)數(shù)據(jù)特征價值的專業(yè)核心 表結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 規(guī)范存儲的結(jié)構(gòu)化數(shù)據(jù),如數(shù)據(jù)庫表、Excel 表、 ...
2025-09-17Excel 導(dǎo)入數(shù)據(jù)含缺失值?詳解 dropna 函數(shù)的功能與實戰(zhàn)應(yīng)用 在用 Python(如 pandas 庫)處理 Excel 數(shù)據(jù)時,“缺失值” 是高頻 ...
2025-09-16深入解析卡方檢驗與 t 檢驗:差異、適用場景與實踐應(yīng)用 在數(shù)據(jù)分析與統(tǒng)計學(xué)領(lǐng)域,假設(shè)檢驗是驗證研究假設(shè)、判斷數(shù)據(jù)差異是否 “ ...
2025-09-16CDA 數(shù)據(jù)分析師:掌控表格結(jié)構(gòu)數(shù)據(jù)全功能周期的專業(yè)操盤手 表格結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 存儲的結(jié)構(gòu)化數(shù)據(jù),如 Excel 表、數(shù)據(jù) ...
2025-09-16MySQL 執(zhí)行計劃中 rows 數(shù)量的準(zhǔn)確性解析:原理、影響因素與優(yōu)化 在 MySQL SQL 調(diào)優(yōu)中,EXPLAIN執(zhí)行計劃是核心工具,而其中的row ...
2025-09-15解析 Python 中 Response 對象的 text 與 content:區(qū)別、場景與實踐指南 在 Python 進(jìn)行 HTTP 網(wǎng)絡(luò)請求開發(fā)時(如使用requests ...
2025-09-15CDA 數(shù)據(jù)分析師:激活表格結(jié)構(gòu)數(shù)據(jù)價值的核心操盤手 表格結(jié)構(gòu)數(shù)據(jù)(如 Excel 表格、數(shù)據(jù)庫表)是企業(yè)最基礎(chǔ)、最核心的數(shù)據(jù)形態(tài) ...
2025-09-15Python HTTP 請求工具對比:urllib.request 與 requests 的核心差異與選擇指南 在 Python 處理 HTTP 請求(如接口調(diào)用、數(shù)據(jù)爬取 ...
2025-09-12解決 pd.read_csv 讀取長浮點數(shù)據(jù)的科學(xué)計數(shù)法問題 為幫助 Python 數(shù)據(jù)從業(yè)者解決pd.read_csv讀取長浮點數(shù)據(jù)時的科學(xué)計數(shù)法問題 ...
2025-09-12CDA 數(shù)據(jù)分析師:業(yè)務(wù)數(shù)據(jù)分析步驟的落地者與價值優(yōu)化者 業(yè)務(wù)數(shù)據(jù)分析是企業(yè)解決日常運營問題、提升執(zhí)行效率的核心手段,其價值 ...
2025-09-12用 SQL 驗證業(yè)務(wù)邏輯:從規(guī)則拆解到數(shù)據(jù)把關(guān)的實戰(zhàn)指南 在業(yè)務(wù)系統(tǒng)落地過程中,“業(yè)務(wù)邏輯” 是連接 “需求設(shè)計” 與 “用戶體驗 ...
2025-09-11塔吉特百貨孕婦營銷案例:數(shù)據(jù)驅(qū)動下的精準(zhǔn)零售革命與啟示 在零售行業(yè) “流量紅利見頂” 的當(dāng)下,精準(zhǔn)營銷成為企業(yè)突圍的核心方 ...
2025-09-11CDA 數(shù)據(jù)分析師與戰(zhàn)略 / 業(yè)務(wù)數(shù)據(jù)分析:概念辨析與協(xié)同價值 在數(shù)據(jù)驅(qū)動決策的體系中,“戰(zhàn)略數(shù)據(jù)分析”“業(yè)務(wù)數(shù)據(jù)分析” 是企業(yè) ...
2025-09-11Excel 數(shù)據(jù)聚類分析:從操作實踐到業(yè)務(wù)價值挖掘 在數(shù)據(jù)分析場景中,聚類分析作為 “無監(jiān)督分組” 的核心工具,能從雜亂數(shù)據(jù)中挖 ...
2025-09-10統(tǒng)計模型的核心目的:從數(shù)據(jù)解讀到?jīng)Q策支撐的價值導(dǎo)向 統(tǒng)計模型作為數(shù)據(jù)分析的核心工具,并非簡單的 “公式堆砌”,而是圍繞特定 ...
2025-09-10CDA 數(shù)據(jù)分析師:商業(yè)數(shù)據(jù)分析實踐的落地者與價值創(chuàng)造者 商業(yè)數(shù)據(jù)分析的價值,最終要在 “實踐” 中體現(xiàn) —— 脫離業(yè)務(wù)場景的分 ...
2025-09-10