最近學(xué)了一點(diǎn)點(diǎn)python爬蟲的知識,面向百度編程爬了一本小說之后感覺有點(diǎn)不滿足,于是突發(fā)奇想嘗試爬一本漫畫下來看看。
一、效果展示
首先是我們想要爬取的漫畫網(wǎng)頁:http://www.manhuadao.cn/
網(wǎng)頁截圖:

其次是爬取下來的效果:

每一回的文件夾里面是這樣的: (因?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)頁鏈接->獲取網(wǎng)頁源碼->定位每一章漫畫的鏈接->模擬點(diǎn)擊->獲取圖片頁面源碼->定位圖片鏈接->下載圖片
三、實(shí)際操作(代碼附在最后)
1、引入模塊 (這里不再詳述)

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

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

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

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

(4)、headers中的參數(shù)為下面紅框User-Agent。
response = requests.get(url=url, headers=headers) # 模擬訪問網(wǎng)頁
print(response) # 此處應(yīng)輸出 Response [200]>
print(response.text) # 輸出網(wǎng)頁源碼
兩個(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)+"頁"
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)頁
'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)頁源碼
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)+"頁"
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 關(guān)于python爬蟲應(yīng)用urllib庫作用分析
- python爬蟲Scrapy框架:媒體管道原理學(xué)習(xí)分析
- python爬蟲Mitmproxy安裝使用學(xué)習(xí)筆記
- Python爬蟲和反爬技術(shù)過程詳解
- python爬蟲之Appium爬取手機(jī)App數(shù)據(jù)及模擬用戶手勢
- 爬蟲Python驗(yàn)證碼識別入門
- Python爬蟲技術(shù)
- Python爬蟲爬取商品失敗處理方法
- Python獲取江蘇疫情實(shí)時(shí)數(shù)據(jù)及爬蟲分析
- Python爬蟲之Scrapy環(huán)境搭建案例教程
- Python爬蟲中urllib3與urllib的區(qū)別是什么
- Python爬蟲分析匯總