回老家去了,顯得無聊上網看個小漫畫,不是那種你們想的~
結果真的是移不動聯不通信不過了,不是一格信號就是無信號,4G變2G了,搞心態!
沒辦法,只能連夜跑去網吧編程,用Python直接把整個網站的漫畫都給爬下來傳手機上了。
哎,這回我就不慌了~
還好我爬的是正經的漫畫,不然后面一堆人,在網吧敲代碼,真是有點特立獨行了。
我回來之后,把這個做了個視頻版教程,大家有需要的可以直接在文末拿,我直接把視頻鏈接發出來了~
好了,簡單的介紹下我們今天的內容叭~
首先咱們用的是這些環境
Python3.6
pycharm
涉及到的知識點
requests
parsel pip install 模塊名
re
os
大概流程
一、單章節爬取
- 找到受害者 https://www.kuimh.com/chapter/332265-3351141
- 向受害者發送請求
- 獲取數據:獲取源代碼
- 解析網頁源代碼: 提取漫畫章節名, 每一頁圖片所在地址
- 保存數據: 圖片數據
二、整本漫畫爬取
- 請求 https://www.kuimh.com/book/mh10575
- 獲取數據:獲取源代碼
- 解析網頁源代碼: 獲取每一話所在地址
- 循環爬取,保存數據
import os import re import parsel import requests
1.正確url地址(靜態\動態)
url = "https://www.kuimh.com/book/mh10575"
獲取動漫章節鏈接和章節名
response = requests.get(url=url)
selector = parsel.Selector(response.text)
偽類選擇器nth-child(第幾個標簽)
::text 獲取文本內容
::attr 獲取標簽屬性值
title_list = selector.css('#detail-list-select li a:nth-child(2)::text').getall() url_list = selector.css('#detail-list-select li a:nth-child(2)::attr(href)').getall() if not os.path.exists('./妖神記/'): os.makedirs('./妖神記/')
f:可以傳入參數
for title, url in zip(title_list, url_list): print(f'--------------------------正在爬取{title}-------------------------') index = 1 target_url = f"https://www.kuimh.com{url}" resp = requests.get(target_url) selector = parsel.Selector(resp.text) sub_url_list = selector.css('.comicpage div img::attr(src)').getall()[:3] sub_list = selector.css('.comicpage div img::attr(data-echo)').getall() for i in sub_list: sub_url_list.append(i)
r:防止字符串轉義
title = re.sub(r'[\/:*?"<>|.]', "", title) if not os.path.exists('./妖神記/' + title): os.makedirs('./妖神記/' + title) for url_ in sub_url_list: image = requests.get(url_).content with open('./妖神記/' + title + '/'+'第'+str(index)+'頁.jpg', mode='wb') as f: f.write(image) print('第', str(index), '頁,爬取成功') index += 1 print(title, '爬取成功!!!')
看不懂的話,建議直接看視頻教程~
看到最后的小伙伴都有福利,給大家送一個VIP音樂下載器,聰明的小伙伴肯定能找到在哪領的(視頻里面)~