python支持多線程的爬蟲(chóng)實(shí)例
python是支持多線程的, 主要是通過(guò)thread和threading這兩個(gè)模塊來(lái)實(shí)現(xiàn)的,本文主要給大家分享python實(shí)現(xiàn)多線程網(wǎng)頁(yè)爬蟲(chóng)
一般來(lái)說(shuō),使用線程有兩種模式, 一種是創(chuàng)建線程要執(zhí)行的函數(shù), 把這個(gè)函數(shù)傳遞進(jìn)Thread對(duì)象里,讓它來(lái)執(zhí)行. 另一種是直接從Thread繼承,創(chuàng)建一個(gè)新的class,把線程執(zhí)行的代碼放到這個(gè)新的class里。
實(shí)現(xiàn)多線程網(wǎng)頁(yè)爬蟲(chóng),采用了多線程和鎖機(jī)制,實(shí)現(xiàn)了廣度優(yōu)先算法的網(wǎng)頁(yè)爬蟲(chóng)。
先給大家簡(jiǎn)單介紹下我的實(shí)現(xiàn)思路:
對(duì)于一個(gè)網(wǎng)絡(luò)爬蟲(chóng),如果要按廣度遍歷的方式下載,它是這樣的:
1.從給定的入口網(wǎng)址把第一個(gè)網(wǎng)頁(yè)下載下來(lái)
2.從第一個(gè)網(wǎng)頁(yè)中提取出所有新的網(wǎng)頁(yè)地址,放入下載列表中
3.按下載列表中的地址,下載所有新的網(wǎng)頁(yè)
4.從所有新的網(wǎng)頁(yè)中找出沒(méi)有下載過(guò)的網(wǎng)頁(yè)地址,更新下載列表
5.重復(fù)3、4兩步,直到更新后的下載列表為空表時(shí)停止
python代碼如下:
#!/usr/bin/env python
#coding=utf-8
import threading
import urllib
import re
import time
g_mutex=threading.Condition()
g_pages=[] #從中解析所有url鏈接
g_queueURL=[] #等待爬取的url鏈接列表
g_existURL=[] #已經(jīng)爬取過(guò)的url鏈接列表
g_failedURL=[] #下載失敗的url鏈接列表
g_totalcount=0 #下載過(guò)的頁(yè)面數(shù)
class Crawler:
def __init__(self,crawlername,url,threadnum):
self.crawlername=crawlername
self.url=url
self.threadnum=threadnum
self.threadpool=[]
self.logfile=file("log.txt",'w')
def craw(self):
global g_queueURL
g_queueURL.append(url)
depth=0
print self.crawlername+" 啟動(dòng)..."
while(len(g_queueURL)!=0):
depth+=1
print 'Searching depth ',depth,'...\n\n'
self.logfile.write("URL:"+g_queueURL[0]+"........")
self.downloadAll()
self.updateQueueURL()
content='\n>>>Depth '+str(depth)+':\n'
self.logfile.write(content)
i=0
while i<len(g_queueURL):
content=str(g_totalcount+i)+'->'+g_queueURL[i]+'\n'
self.logfile.write(content)
i+=1
def downloadAll(self):
global g_queueURL
global g_totalcount
i=0
while i<len(g_queueURL):
j=0
while j<self.threadnum and i+j < len(g_queueURL):
g_totalcount+=1
threadresult=self.download(g_queueURL[i+j],str(g_totalcount)+'.html',j)
if threadresult!=None:
print 'Thread started:',i+j,'--File number =',g_totalcount
j+=1
i+=j
for thread in self.threadpool:
thread.join(30)
threadpool=[]
g_queueURL=[]
def download(self,url,filename,tid):
crawthread=CrawlerThread(url,filename,tid)
self.threadpool.append(crawthread)
crawthread.start()
def updateQueueURL(self):
global g_queueURL
global g_existURL
newUrlList=[]
for content in g_pages:
newUrlList+=self.getUrl(content)
g_queueURL=list(set(newUrlList)-set(g_existURL))
def getUrl(self,content):
reg=r'"(http://.+?)"'
regob=re.compile(reg,re.DOTALL)
urllist=regob.findall(content)
return urllist
class CrawlerThread(threading.Thread):
def __init__(self,url,filename,tid):
threading.Thread.__init__(self)
self.url=url
self.filename=filename
self.tid=tid
def run(self):
global g_mutex
global g_failedURL
global g_queueURL
try:
page=urllib.urlopen(self.url)
html=page.read()
fout=file(self.filename,'w')
fout.write(html)
fout.close()
except Exception,e:
g_mutex.acquire()
g_existURL.append(self.url)
g_failedURL.append(self.url)
g_mutex.release()
print 'Failed downloading and saving',self.url
print e
return None
g_mutex.acquire()
g_pages.append(html)
g_existURL.append(self.url)
g_mutex.release()
if __name__=="__main__":
url=raw_input("請(qǐng)輸入url入口:\n")
threadnum=int(raw_input("設(shè)置線程數(shù):"))
crawlername="小小爬蟲(chóng)"
crawler=Crawler(crawlername,url,threadnum)
crawler.craw()
以上這篇python支持多線程的爬蟲(chóng)實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python基礎(chǔ)進(jìn)階之海量表情包多線程爬蟲(chóng)功能的實(shí)現(xiàn)
- python3爬蟲(chóng)中多線程進(jìn)行解鎖操作實(shí)例
- python3爬蟲(chóng)GIL修改多線程實(shí)例講解
- python3爬蟲(chóng)中多線程的優(yōu)勢(shì)總結(jié)
- python爬蟲(chóng)開(kāi)發(fā)之使用Python爬蟲(chóng)庫(kù)requests多線程抓取貓眼電影TOP100實(shí)例
- python爬蟲(chóng)中多線程的使用詳解
- Python 微信爬蟲(chóng)完整實(shí)例【單線程與多線程】
- python面向?qū)ο蠖嗑€程爬蟲(chóng)爬取搜狐頁(yè)面的實(shí)例代碼
- python爬蟲(chóng)爬取快手視頻多線程下載功能
- Python3多線程爬蟲(chóng)實(shí)例講解代碼
- php與python實(shí)現(xiàn)的線程池多線程爬蟲(chóng)功能示例
- python 多線程爬取壁紙網(wǎng)站的示例
相關(guān)文章
Python實(shí)現(xiàn)DBSCAN聚類(lèi)算法并樣例測(cè)試
聚類(lèi)是一種機(jī)器學(xué)習(xí)技術(shù),它涉及到數(shù)據(jù)點(diǎn)的分組,聚類(lèi)是一種無(wú)監(jiān)督學(xué)習(xí)的方法,是許多領(lǐng)域中常用的統(tǒng)計(jì)數(shù)據(jù)分析技術(shù)。本文給大家分享Python實(shí)現(xiàn)DBSCAN聚類(lèi)算法并樣例測(cè)試,感興趣的朋友一起看看吧2021-06-06
python實(shí)現(xiàn)的接收郵件功能示例【基于網(wǎng)易POP3服務(wù)器】
這篇文章主要介紹了python實(shí)現(xiàn)的接收郵件功能,結(jié)合實(shí)例形式分析了Python基于網(wǎng)易POP3服務(wù)器接收郵件相關(guān)操作技巧,需要的朋友可以參考下2019-09-09
Python?if?else語(yǔ)句對(duì)縮進(jìn)的要求
這篇文章主要介紹了Python?if?else語(yǔ)句對(duì)縮進(jìn)的要求,前面的一篇文章展示了選擇結(jié)構(gòu)的三種基本形式,并給出了實(shí)例演示,這篇文章基于上一篇內(nèi)容繼續(xù)對(duì)Python?if?else語(yǔ)句對(duì)縮進(jìn)進(jìn)行描述,需要的小伙伴可以參考一下2022-03-03
Python實(shí)現(xiàn)AI自動(dòng)摳圖實(shí)例解析
這篇文章主要介紹了Python實(shí)現(xiàn)AI自動(dòng)摳圖實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Django中使用極驗(yàn)Geetest滑動(dòng)驗(yàn)證碼過(guò)程解析
這篇文章主要介紹了Django中使用極驗(yàn)Geetest滑動(dòng)驗(yàn)證碼過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
用python對(duì)excel進(jìn)行操作(讀,寫(xiě),修改)
這篇文章主要介紹了用python對(duì)excel進(jìn)行操作(讀,寫(xiě),修改),幫助大家更好的利用python處理表格,感興趣的朋友可以了解下2020-12-12

