python爬蟲項(xiàng)目設(shè)置一個中斷重連的程序的實(shí)現(xiàn)
做爬蟲項(xiàng)目時,我們需要考慮一個爬蟲在爬取時會遇到各種情況(網(wǎng)站驗(yàn)證,ip封禁),導(dǎo)致爬蟲程序中斷,這時我們已經(jīng)爬取過一些數(shù)據(jù),再次爬取時這些數(shù)據(jù)就可以忽略,所以我們需要在爬蟲項(xiàng)目中設(shè)置一個中斷重連的功能,使其在重新運(yùn)行時從之前斷掉的位置重新爬取數(shù)據(jù)。
實(shí)現(xiàn)該功能有很多種做法,我自己就有好幾種思路,但是真要自己寫出來就要費(fèi)很大的功夫,下面我就把自己好不容易拼湊出來的代碼展示出來吧。
首先是來介紹代碼的思路:
將要爬取的網(wǎng)站連接存在一個數(shù)組new_urls中,爬取一個網(wǎng)址就將它移入另一個數(shù)組old_urls中,爬取網(wǎng)站時,就看它是在哪一個數(shù)組中,然后再決定要不要爬取。
下面展示代碼(從別處抄的):
class UrlManager(object):
def __init__(self): #定義兩個數(shù)組
self.new_urls=set()
self.old_urls=set()
def add_new_url(self, url): #將一個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): #將多個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)移除列表中最后一個元素對象
new_url=self.new_urls.pop()
self.old_urls.add(new_url)
return new_url
這個類實(shí)現(xiàn)了中斷重連的基本功能,但是當(dāng)我們要爬取的網(wǎng)址非常的,那這就對我們電腦的內(nèi)存要求非常大,所以我們要將數(shù)組保存到文檔中,增加一個從文檔中提取網(wǎng)址的過程。
下面看代碼:
class UrlManager(object):
def __init__(self): #建立兩個數(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): #添加多個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數(shù)組中
return new_url
其中的get_last_line()函數(shù)有些復(fù)雜,這也是我卡時間最長的一塊,
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,但第二個元素卻還是空行
# 如果跳出循環(huán),那么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 # 已經(jīng)找到最后一個不是空行的
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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python?字符串模糊匹配Fuzzywuzzy的實(shí)現(xiàn)
本文主要介紹了python?字符串模糊匹配Fuzzywuzzy的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
使用python3 實(shí)現(xiàn)插入數(shù)據(jù)到mysql
今天小編就為大家分享一篇使用python3 實(shí)現(xiàn)插入數(shù)據(jù)到mysql,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
這篇文章主要為大家介紹了python中selenium模塊的安裝和配置環(huán)境變量教程、提取數(shù)據(jù)操作、無頭模式,有需要的朋友可以借鑒參考下,希望能夠?qū)Υ蠹矣兴鶐椭?/div> 2022-10-10
python人工智能算法之差分進(jìn)化算法的實(shí)現(xiàn)
DE基于GA,正如進(jìn)化基于遺傳,和遺傳算法相比,差分進(jìn)化引入了差分變異模式,相當(dāng)于開辟了一條嶄新的進(jìn)化路徑,下面就來看看差分優(yōu)化算法是如何實(shí)現(xiàn)的吧2023-08-08
python深度學(xué)習(xí)人工智能BackPropagation鏈?zhǔn)椒▌t
這篇文章主要為大家介紹了python深度學(xué)習(xí)人工智能BackPropagation鏈?zhǔn)椒▌t的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11
使用Matplotlib創(chuàng)建漂亮的數(shù)據(jù)可視化圖表
在 Python 中,Matplotlib 是一個強(qiáng)大而靈活的工具,可以用來創(chuàng)建各種類型的數(shù)據(jù)可視化圖表,本文給大家介紹了如何使用Matplotlib創(chuàng)建漂亮的數(shù)據(jù)可視化圖表,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-04-04最新評論

