最近學(xué)了一點(diǎn)點(diǎn)python爬蟲的知識(shí),面向百度編程爬了一本小說之后感覺有點(diǎn)不滿足,于是突發(fā)奇想嘗試爬一本漫畫下來看看。
首先是我們想要爬取的漫畫網(wǎng)頁(yè):http://www.manhuadao.cn/
網(wǎng)頁(yè)截圖:
其次是爬取下來的效果:
每一回的文件夾里面是這樣的: (因?yàn)榫W(wǎng)站圖片的問題...所以就成了這個(gè)鬼樣子)
1、準(zhǔn)備:需要vscode或者其他能夠編譯運(yùn)行python的軟件,推薦python版本3.X ,否則有可能出現(xiàn)編譯問題。
下載所需模塊:win+R進(jìn)入命令行,輸入pipinstall 模塊名>即可下載。例如:
pip install beautifulsoup4
2、原理: 模擬瀏覽器點(diǎn)擊->打開漫畫網(wǎng)頁(yè)鏈接->獲取網(wǎng)頁(yè)源碼->定位每一章漫畫的鏈接->模擬點(diǎn)擊->獲取圖片頁(yè)面源碼->定位圖片鏈接->下載圖片
1、引入模塊 (這里不再詳述)
2、模擬瀏覽器訪問網(wǎng)頁(yè)
(1)、這里我們打開漫畫的目錄頁(yè),如下: url = ”http://www.manhuadao.cn/Home/ComicDetail?id=58ddb07827a7c1392c234628“ ,此鏈接就是目錄頁(yè)鏈接。
(2)、按F12打開此網(wǎng)頁(yè)的源碼(谷歌瀏覽器),選中上方NetWork,Ctrl+R刷新。
(3)、找到加載網(wǎng)頁(yè)的源碼文件,點(diǎn)擊Headers,如下圖: StatusCode表示網(wǎng)頁(yè)返回的代碼,值為200時(shí)表示訪問成功。
(4)、headers中的參數(shù)為下面紅框User-Agent。
response = requests.get(url=url, headers=headers) # 模擬訪問網(wǎng)頁(yè) print(response) # 此處應(yīng)輸出 Response [200]> print(response.text) # 輸出網(wǎng)頁(yè)源碼
兩個(gè)輸出分別輸出:
輸出返回200表示訪問成功。
(節(jié)選)
(5)、將html代碼存入 data 中,xpath定位每一章鏈接。點(diǎn)擊上方Element,點(diǎn)擊:
將鼠標(biāo)移至目錄處:
右邊代碼區(qū)域出現(xiàn)每一章鏈接:
data = etree.HTML(response.text) # tp = data.xpath('//ul[@class="read-chapter"]/li/a[@class="active"]/@href') tp = data.xpath('//*[@class="yesReader"]/@href') zhang_list = tp # tp為鏈接列表
輸出zhang_list,結(jié)果如下:
(6)、獲取圖片鏈接(獲取方式同上一步)
點(diǎn)進(jìn)第一章,同上一步,尋找到圖片鏈接:
i=1 for next_zhang in zhang_list: # 在章節(jié)列表中循環(huán) i=i+1 j=0 hui_url = r_url+next_zhang name1 = "第"+str(i)+"回" file = 'C:/Users/wangyueke/Desktop/'+keyword+'/{}/'.format(name1) # 創(chuàng)建文件夾 if not os.path.exists(file): os.makedirs(file) print('創(chuàng)建文件夾:', file) response = requests.get(url=hui_url, headers=headers) # 模擬訪問每一章鏈接 data = etree.HTML(response.text) # tp = data.xpath('//div[@class="no-pic"]//img/@src') tp = data.xpath('//div[@class="main-content"]//ul//li//div[@class="no-pic"]//img/@src') # 定位 ye_list = tp
(7)、下載圖片
for k in ye_list: # 在每一章的圖片鏈接列表中循環(huán) download_url = tp[j] print(download_url) j=j+1 file_name="第"+str(j)+"頁(yè)" response = requests.get(url=download_url) # 模擬訪問圖片鏈接 with open(file+file_name+".jpg","wb") as f: f.write(response.content)
''' 用于爬取非人哉漫畫 目標(biāo)網(wǎng)址:http://www.manhuadao.cn/ 開始時(shí)間:2019/8/14 20:01:26 完成時(shí)間:2019/8/15 11:04:56 作者:kong_gu ''' import requests import json import time import os from lxml import etree from bs4 import BeautifulSoup def main(): keyword="非人哉" file = 'E:/{}'.format(keyword) if not os.path.exists(file): os.mkdir(file) print('創(chuàng)建文件夾:',file) r_url="http://www.manhuadao.cn/" url = "http://www.manhuadao.cn/Home/ComicDetail?id=58ddb07827a7c1392c234628" headers = { # 模擬瀏覽器訪問網(wǎng)頁(yè) 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) \\Chrome/75.0.3770.142 Safari/537.36'} response = requests.get(url=url, headers=headers) # print(response.text) # 輸出網(wǎng)頁(yè)源碼 data = etree.HTML(response.text) # tp = data.xpath('//ul[@class="read-chapter"]/li/a[@class="active"]/@href') tp = data.xpath('//*[@class="yesReader"]/@href') zhang_list = tp i=1 for next_zhang in zhang_list: i=i+1 j=0 hui_url = r_url+next_zhang name1 = "第"+str(i)+"回" file = 'C:/Users/wangyueke/Desktop/'+keyword+'/{}/'.format(name1) # 這里需要自己設(shè)置路徑 if not os.path.exists(file): os.makedirs(file) print('創(chuàng)建文件夾:', file) response = requests.get(url=hui_url, headers=headers) data = etree.HTML(response.text) # tp = data.xpath('//div[@class="no-pic"]//img/@src') tp = data.xpath('//div[@class="main-content"]//ul//li//div[@class="no-pic"]//img/@src') ye_list = tp for k in ye_list: download_url = tp[j] print(download_url) j=j+1 file_name="第"+str(j)+"頁(yè)" response = requests.get(url=download_url) with open(file+file_name+".jpg","wb") as f: f.write(response.content) if __name__ == '__main__': main()
到此這篇關(guān)于利用python3爬蟲爬取漫畫島-非人哉漫畫的文章就介紹到這了,更多相關(guān)python3爬蟲漫畫島內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:盤錦 西寧 湖北 佳木斯 宜昌 潮州 上饒 珠海
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《教你如何利用python3爬蟲爬取漫畫島-非人哉漫畫》,本文關(guān)鍵詞 教你,如何,利用,python3,爬蟲,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。