本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,著作權歸原作者所有,如有問題請及時聯系我們以作處理,
轉載地址
https://blog.csdn.net/fei347795790?t=1
某年某月某日,重溫某本漫畫,心血來潮,爬之!!!
分析思路:
1. 通過靜態爬取漫畫所有章節的url鏈接資訊以及集數資訊,
2. 通過動態爬取每集漫畫的所有篇幅,
3. 每集漫畫保存至相應本地檔案夾中,
一、獲取所有漫畫資訊
通過對漫畫網頁地址load = "http://x.seeym.net/xxxx/xxxxxxxx/"分析,每集漫畫 的鏈接和集數集中于某個標簽中,通過requests庫和BeautifulSoup庫獲取該標簽資訊,并轉化成pd.DataFrame格式資料,共包含三列ID(集數),url(漫畫鏈接),name(集名),.
import os
import numpy as npimport pandas as pdimport requestsimport reimport timefrom bs4 import BeautifulSoup
from selenium import webdriver
#載入漫畫鏈接,回傳DataFrame檔案,包含ID,URL鏈接,檔案名.def url(load): try: bb = requests.get(load, timeout = 30)
bb.encoding = bb.apparent_encoding soup = BeautifulSoup(bb.text,'lxml')
div = soup.find(id = 'mh-chapter-list-ol-0')
url = [] name = [] for i in div.find_all("a"):
url.append("http://m.seeym.net" + i.attrs["href"])
b = re.sub(u"([^\u4e00-\u9fa5\u0030-\u0039\u0041-\u005a\u0061-\u007a])","",i.string)
name.append(b)
ID = np.array(list(reversed(range(len(url))))).reshape(len(url),1) + 1
url = np.array(url).reshape(len(url),1)
name = np.array(name).reshape(len(url),1)
info = pd.DataFrame(np.concatenate((ID,url,name),axis=1),columns=["ID","url","name"])
info["ID"] = info["ID"].apply(pd.to_numeric)
info = info.sort_values(by = "ID")
return info
except: print("網頁不可用")
二、下載漫畫
url鏈接分析:具體漫畫資訊隱藏在動態鏈接中,通過selenium爬取圖片資訊和漫畫圖片數,每張圖片的url鏈接為http://xxxxxx.html?p=i,i為漫畫圖片數,第一步,獲得該集漫畫圖片總數,第二步,回圈爬取漫畫圖片,第三步,下載保存,
#提取漫畫內容load:載入漫畫網址,path:保存地址,dic:保存集數
def jpg(load,path,dic,browser,ID):
a = 1
try: browser.get(load)
elemt = browser.find_element_by_id("k_total")
total = elemt.text for i in range(1,int(total)+1):
load1 = load + '?p=' + str(i)
browser.get(load1) elemt = browser.find_element_by_id("qTcms_pic")
bb1 = elemt.get_attribute('src')
bb2 = requests.get(bb1, timeout = 30)
bb2.encoding = bb2.apparent_encoding path1 = path + '/' + str(ID) + '_' + dic + '/' + str(i) + '.jpg'
with open(path1, 'wb') as f:
f.write(bb2.content)
time.sleep(np.random.randint(1,3))
print('xxxxxx{}下載成功'.format(dic))
return a - 1
except: print('xxxxxx{}下載失敗'.format(dic))
return a
代碼修改:下載程序中出現漫畫集名有特殊字符的情況,通過正則運算式將特殊字符剔除,
三、新建保存檔案夾和洗掉檔案夾
更新漫畫操作,若本地檔案夾中無該漫畫集數子檔案夾,則創建檔案夾,若有,則不創建,洗掉下載失敗的檔案夾,若不能下載完整漫畫集,則洗掉創建的檔案夾,
#新建檔案夾
def mkdir(path,dic,ID):
path = path + '/' + str(ID) + '_' + dic
check = 1
if os.path.exists(path) == False:
print('創建{}檔案夾'.format(dic))
os.mkdir(path)
return check - 1
else:
return check
#洗掉檔案夾,檔案def deldir(path,dic,ID):
path = path + '/' + str(ID) + '_' + dic
if os.path.exists(path) == True:
for i in os.listdir(path):
path1 = path + '/' + i
os.remove(path1)
os.rmdir(path)
else:
pass
四、拼接代碼
下載流程:
1. 輸入load鏈接,本地地址,運行webdriver.Chrome谷歌瀏覽器,
2. 獲取漫畫url資訊,
3. 回圈下載漫畫,生成下載失敗檔案fail,
4. 新建子檔案下,下載漫畫,并time.sleep(np.random.randint(x))模擬人看漫畫時長,
def main():
#第一步:載入xxxxx網址和保存漫畫地址 browser = webdriver.Chrome(executable_path = "D:/chromedriver.exe")
load = "http://xxxxx.net/xxxx/xxxxx/"
path = "F:/漫畫/xxxxx"
#第二步,獲取所有漫畫的url資訊 info = url(load)
#第三步,下載漫畫 fail = info.iloc[0:1,:]
for i in range(len(info)):
http = info.iloc[i,1]
dic = info.iloc[i,2]
ID = info.iloc[i,0]
check = mkdir(path,dic,ID)
if check == 0:
a = jpg(http,path,dic,browser,ID)
if a == 1:
fail = pd.concat((fail,info.iloc[i:i+1,:]))
deldir(path,dic,ID)
else:
time.sleep(np.random.randint(3,6))
continue else:
continue return fail
fail = main()
五、結果
PS:網址來源于某盜版網址,圖文中相關鏈接等資訊均隱藏,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/114498.html
標籤:其他
上一篇:用 python print() 函式實作的三個特效
下一篇:968監控二叉樹
