做爬蟲項(xiàng)目時(shí),我們需要考慮一個(gè)爬蟲在爬取時(shí)會(huì)遇到各種情況(網(wǎng)站驗(yàn)證,ip封禁),導(dǎo)致爬蟲程序中斷,這時(shí)我們已經(jīng)爬取過一些數(shù)據(jù),再次爬取時(shí)這些數(shù)據(jù)就可以忽略,所以我們需要在爬蟲項(xiàng)目中設(shè)置一個(gè)中斷重連的功能,使其在重新運(yùn)行時(shí)從之前斷掉的位置重新爬取數(shù)據(jù)。
實(shí)現(xiàn)該功能有很多種做法,我自己就有好幾種思路,但是真要自己寫出來就要費(fèi)很大的功夫,下面我就把自己好不容易拼湊出來的代碼展示出來吧。
首先是來介紹代碼的思路:
將要爬取的網(wǎng)站連接存在一個(gè)數(shù)組new_urls中,爬取一個(gè)網(wǎng)址就將它移入另一個(gè)數(shù)組old_urls中,爬取網(wǎng)站時(shí),就看它是在哪一個(gè)數(shù)組中,然后再?zèng)Q定要不要爬取。
下面展示代碼(從別處抄的):
class UrlManager(object): def __init__(self): #定義兩個(gè)數(shù)組 self.new_urls=set() self.old_urls=set() def add_new_url(self, url): #將一個(gè)url加入到new_urls數(shù)組中 if url is None: return if url not in self.new_urls and url not in self.old_urls: self.new_urls.add(url) def add_new_urls(self, urls): #將多個(gè)url加入到new_urls數(shù)組中 if urls is None or len(urls)==0: return for url in urls : self.add_new_url(url) def has_new_url(self): #判斷url是否為空 return len(self.new_urls)!=0 def get_new_url(self): #list.pop()默認(rèn)移除列表中最后一個(gè)元素對(duì)象 new_url=self.new_urls.pop() self.old_urls.add(new_url) return new_url
這個(gè)類實(shí)現(xiàn)了中斷重連的基本功能,但是當(dāng)我們要爬取的網(wǎng)址非常的,那這就對(duì)我們電腦的內(nèi)存要求非常大,所以我們要將數(shù)組保存到文檔中,增加一個(gè)從文檔中提取網(wǎng)址的過程。
下面看代碼:
class UrlManager(object): def __init__(self): #建立兩個(gè)數(shù)組的文件 with open('new_urls.txt','r+') as new_urls: self.new_urls = new_urls.read() with open('old_urls.txt','r+') as old_urls: self.old_urls = old_urls.read() def add_new_url(self, url): #添加url到new_ulrs文件中 if url is None: return if url not in self.new_urls and url not in self.old_urls: with open('new_urls.txt', 'a') as new_urls: new_urls.write(url) else: print('url had done') def add_new_urls(self, urls): #添加多個(gè)url到new_ulrs文件中 # if urls is None or (len(url) == 0 for url in urls): if urls is None: print('url is none') return for url in urls: if urls is None: print('url is none') return else: self.add_new_url(url) def has_new_url(self): return len(self.new_urls) != 0 def get_new_url(self): new_url = get_last_line('new_urls.txt') #讀取new_urls文件中最后一個(gè)url del_last_url('new_urls.txt',new_url) #刪除new_urls文件中最后一個(gè)url add_old_urls('old_urls.txt',new_url) #將讀取出來的url添加入old_urls數(shù)組中 return new_url
其中的get_last_line()函數(shù)有些復(fù)雜,這也是我卡時(shí)間最長的一塊,
import os def get_last_line(inputfile): filesize = os.path.getsize(inputfile) blocksize = 1024 dat_file = open(inputfile, 'rb') last_line = b"" lines = [] if filesize > blocksize: maxseekpoint = (filesize // blocksize) # 這里的除法取的是floor maxseekpoint -= 1 dat_file.seek(maxseekpoint * blocksize) lines = dat_file.readlines() while ((len(lines) < 2) | ((len(lines) >= 2) & (lines[1] == b'\r\n'))): # 因?yàn)樵赪indows下,所以是b'\r\n' # 如果列表長度小于2,或者雖然長度大于等于2,但第二個(gè)元素卻還是空行 # 如果跳出循環(huán),那么lines長度大于等于2,且第二個(gè)元素肯定是完整的行 maxseekpoint -= 1 dat_file.seek(maxseekpoint * blocksize) lines = dat_file.readlines() elif filesize: # 文件大小不為空 dat_file.seek(0, 0) lines = dat_file.readlines() if lines: # 列表不為空 for i in range(len(lines) - 1, -1, -1): last_line = lines[i].strip() if (last_line != b''): break # 已經(jīng)找到最后一個(gè)不是空行的 dat_file.close() return last_line def del_last_url(fname,part): with open(fname,'rb+') as f: a = f.read() a = a.replace(part,b'') with open(fname,'wb+') as f: f.write(a) def add_old_urls(fname,new_url): line = new_url + b'\r' with open(fname,'ab') as f: f.write(line)
好了,爬蟲的中斷重連的功能就實(shí)現(xiàn)了,下面要做的就是將該功能接入爬蟲項(xiàng)目中,比較簡單。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
