做爬蟲項目時,我們需要考慮一個爬蟲在爬取時會遇到各種情況(網站驗證,ip封禁),導致爬蟲程序中斷,這時我們已經爬取過一些數據,再次爬取時這些數據就可以忽略,所以我們需要在爬蟲項目中設置一個中斷重連的功能,使其在重新運行時從之前斷掉的位置重新爬取數據。
實現該功能有很多種做法,我自己就有好幾種思路,但是真要自己寫出來就要費很大的功夫,下面我就把自己好不容易拼湊出來的代碼展示出來吧。
首先是來介紹代碼的思路:
將要爬取的網站連接存在一個數組new_urls中,爬取一個網址就將它移入另一個數組old_urls中,爬取網站時,就看它是在哪一個數組中,然后再決定要不要爬取。
下面展示代碼(從別處抄的):
class UrlManager(object): def __init__(self): #定義兩個數組 self.new_urls=set() self.old_urls=set() def add_new_url(self, url): #將一個url加入到new_urls數組中 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): #將多個url加入到new_urls數組中 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()默認移除列表中最后一個元素對象 new_url=self.new_urls.pop() self.old_urls.add(new_url) return new_url
這個類實現了中斷重連的基本功能,但是當我們要爬取的網址非常的,那這就對我們電腦的內存要求非常大,所以我們要將數組保存到文檔中,增加一個從文檔中提取網址的過程。
下面看代碼:
class UrlManager(object): def __init__(self): #建立兩個數組的文件 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): #添加多個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文件中最后一個url del_last_url('new_urls.txt',new_url) #刪除new_urls文件中最后一個url add_old_urls('old_urls.txt',new_url) #將讀取出來的url添加入old_urls數組中 return new_url
其中的get_last_line()函數有些復雜,這也是我卡時間最長的一塊,
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'))): # 因為在Windows下,所以是b'\r\n' # 如果列表長度小于2,或者雖然長度大于等于2,但第二個元素卻還是空行 # 如果跳出循環,那么lines長度大于等于2,且第二個元素肯定是完整的行 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 # 已經找到最后一個不是空行的 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)
好了,爬蟲的中斷重連的功能就實現了,下面要做的就是將該功能接入爬蟲項目中,比較簡單。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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