教你如何利用python3爬蟲(chóng)爬取漫畫島-非人哉漫畫
最近學(xué)了一點(diǎn)點(diǎn)python爬蟲(chóng)的知識(shí),面向百度編程爬了一本小說(shuō)之后感覺(jué)有點(diǎn)不滿足,于是突發(fā)奇想嘗試爬一本漫畫下來(lái)看看。
一、效果展示
首先是我們想要爬取的漫畫網(wǎng)頁(yè):http://www.manhuadao.cn/
網(wǎng)頁(yè)截圖:

其次是爬取下來(lái)的效果:

每一回的文件夾里面是這樣的: (因?yàn)榫W(wǎng)站圖片的問(wèn)題...所以就成了這個(gè)鬼樣子)

二、分析原理
1、準(zhǔn)備:需要vscode或者其他能夠編譯運(yùn)行python的軟件,推薦python版本3.X ,否則有可能出現(xiàn)編譯問(wèn)題。
下載所需模塊:win+R進(jìn)入命令行,輸入pipinstall <模塊名>即可下載。例如:
pip install beautifulsoup4
2、原理: 模擬瀏覽器點(diǎn)擊->打開(kāi)漫畫網(wǎng)頁(yè)鏈接->獲取網(wǎng)頁(yè)源碼->定位每一章漫畫的鏈接->模擬點(diǎn)擊->獲取圖片頁(yè)面源碼->定位圖片鏈接->下載圖片
三、實(shí)際操作(代碼附在最后)
1、引入模塊 (這里不再詳述)

2、模擬瀏覽器訪問(wèn)網(wǎng)頁(yè)

(1)、這里我們打開(kāi)漫畫的目錄頁(yè),如下: url = ”http://www.manhuadao.cn/Home/ComicDetail?id=58ddb07827a7c1392c234628“ ,此鏈接就是目錄頁(yè)鏈接。

(2)、按F12打開(kāi)此網(wǎng)頁(yè)的源碼(谷歌瀏覽器),選中上方NetWork,Ctrl+R刷新。

(3)、找到加載網(wǎng)頁(yè)的源碼文件,點(diǎn)擊Headers,如下圖: StatusCode表示網(wǎng)頁(yè)返回的代碼,值為200時(shí)表示訪問(wèn)成功。

(4)、headers中的參數(shù)為下面紅框User-Agent。
response = requests.get(url=url, headers=headers) # 模擬訪問(wèn)網(wǎng)頁(yè) print(response) # 此處應(yīng)輸出 <Response [200]> print(response.text) # 輸出網(wǎng)頁(yè)源碼
兩個(gè)輸出分別輸出:
輸出返回200表示訪問(wèn)成功。
(節(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) # 模擬訪問(wèn)每一章鏈接
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) # 模擬訪問(wèn)圖片鏈接
with open(file+file_name+".jpg","wb") as f:
f.write(response.content)
五、代碼
'''
用于爬取非人哉漫畫
目標(biāo)網(wǎng)址:http://www.manhuadao.cn/
開(kāi)始時(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èn)網(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爬蟲(chóng)爬取漫畫島-非人哉漫畫的文章就介紹到這了,更多相關(guān)python3爬蟲(chóng)漫畫島內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 關(guān)于python爬蟲(chóng)應(yīng)用urllib庫(kù)作用分析
- python爬蟲(chóng)Scrapy框架:媒體管道原理學(xué)習(xí)分析
- python爬蟲(chóng)Mitmproxy安裝使用學(xué)習(xí)筆記
- Python爬蟲(chóng)和反爬技術(shù)過(guò)程詳解
- python爬蟲(chóng)之Appium爬取手機(jī)App數(shù)據(jù)及模擬用戶手勢(shì)
- 爬蟲(chóng)Python驗(yàn)證碼識(shí)別入門
- Python爬蟲(chóng)技術(shù)
- Python爬蟲(chóng)爬取商品失敗處理方法
- Python獲取江蘇疫情實(shí)時(shí)數(shù)據(jù)及爬蟲(chóng)分析
- Python爬蟲(chóng)之Scrapy環(huán)境搭建案例教程
- Python爬蟲(chóng)中urllib3與urllib的區(qū)別是什么
- Python爬蟲(chóng)分析匯總
相關(guān)文章
python中Requests發(fā)送json格式的post請(qǐng)求方法
這篇文章主要介紹了python中Requests發(fā)送json格式的post請(qǐng)求方法,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的朋友可以參考一下2022-09-09
Python 獲取命令行參數(shù)內(nèi)容及參數(shù)個(gè)數(shù)的實(shí)例
今天小編就為大家分享一篇Python 獲取命令行參數(shù)內(nèi)容及參數(shù)個(gè)數(shù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
使用Python3+PyQT5+Pyserial 實(shí)現(xiàn)簡(jiǎn)單的串口工具方法
今天小編就為大家分享一篇使用Python3+PyQT5+Pyserial 實(shí)現(xiàn)簡(jiǎn)單的串口工具方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Python 詳解通過(guò)Scrapy框架實(shí)現(xiàn)爬取百度新冠疫情數(shù)據(jù)流程
Scrapy是用純Python實(shí)現(xiàn)一個(gè)為了爬取網(wǎng)站數(shù)據(jù)、提取結(jié)構(gòu)性數(shù)據(jù)而編寫的應(yīng)用框架,用途非常廣泛,框架的力量,用戶只需要定制開(kāi)發(fā)幾個(gè)模塊就可以輕松的實(shí)現(xiàn)一個(gè)爬蟲(chóng),用來(lái)抓取網(wǎng)頁(yè)內(nèi)容以及各種圖片,非常之方便2021-11-11

