python爬蟲項目設(shè)置一個中斷重連的程序的實現(xiàn)
做爬蟲項目時,我們需要考慮一個爬蟲在爬取時會遇到各種情況(網(wǎng)站驗證,ip封禁),導致爬蟲程序中斷,這時我們已經(jīng)爬取過一些數(shù)據(jù),再次爬取時這些數(shù)據(jù)就可以忽略,所以我們需要在爬蟲項目中設(shè)置一個中斷重連的功能,使其在重新運行時從之前斷掉的位置重新爬取數(shù)據(jù)。
實現(xiàn)該功能有很多種做法,我自己就有好幾種思路,但是真要自己寫出來就要費很大的功夫,下面我就把自己好不容易拼湊出來的代碼展示出來吧。
首先是來介紹代碼的思路:
將要爬取的網(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()默認移除列表中最后一個元素對象
new_url=self.new_urls.pop()
self.old_urls.add(new_url)
return new_url
這個類實現(xiàn)了中斷重連的基本功能,但是當我們要爬取的網(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ù)有些復雜,這也是我卡時間最長的一塊,
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,但第二個元素卻還是空行
# 如果跳出循環(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)
好了,爬蟲的中斷重連的功能就實現(xiàn)了,下面要做的就是將該功能接入爬蟲項目中,比較簡單。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python語音合成之第三方庫gTTs/pyttsx3/speech橫評(內(nèi)附使用方法)
Python是一種非常強大的腳本語言,可以用來實現(xiàn)各種復雜的應用,其中之一就是文本轉(zhuǎn)語音,即把文字轉(zhuǎn)換成聲音來發(fā)出,下面這篇文章主要給大家介紹了關(guān)于Python語音合成之第三方庫gTTs/pyttsx3/speech橫評的相關(guān)資料,文中還介紹了詳細的使用方法,需要的朋友可以參考下2023-05-05
tensorflow ckpt模型和pb模型獲取節(jié)點名稱,及ckpt轉(zhuǎn)pb模型實例
今天小編就為大家分享一篇tensorflow ckpt模型和pb模型獲取節(jié)點名稱,及ckpt轉(zhuǎn)pb模型實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Pytorch抽取網(wǎng)絡層的Feature Map(Vgg)實例
今天小編就為大家分享一篇Pytorch抽取網(wǎng)絡層的Feature Map(Vgg)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08

