前因后果
公司新來的小姐姐,超級喜歡看漫畫,天天給我介紹,好煩~
現在是2022年9月15日16點30,于是我決定, 五點下班前寫個代碼把她說的漫畫全部爬下來,應付一下~
再發篇文章揭露她的罪惡,嘿嘿~

準備事項
環境使用
Python 3.8
Pycharm 2021.2版本
模塊使用
import requests >>> # 資料請求模塊 pip install requests import re # 正則模塊 import os # 檔案操作模塊
基本流程思路
一. 資料來源分析
1. 確定自己需求:
采集那個網站上面什么資料內容
https://www.dongmanmanhua.cn 網址自己補全一下,實在不知道的話文末見,
正常訪問流程:
1. 選中漫畫 ---> 目錄頁面 <請求串列頁面 獲取所有章節鏈接>
2. 選擇一個漫畫內容 ---> 漫畫頁面 <請求章節鏈接, 獲取所有漫畫內容url>
3. 看漫畫內容 <保存資料, 漫畫圖片內容保存下來>
分析流程: <開發者工具進行抓包分析>
1. 查看漫畫圖片url地址, 是什么樣子
https://cdn.dongmanmanhua.cn/166052717362315191169.jpg?x-oss-process=image/quality,q_90
2. 分析url地址在哪里
通過搜索功能 <開發者工具> 166052717362315191169
https://www.dongmanmanhua.cn/BOY/moutianchengweimoshen/116-%E7%AC%AC43%E7%AB%A0-%E5%A2%9E%E5%8A%A0%E6%88%98%E6%96%97%E5%8A%9B%E5%90%A73/viewer?title_no=1519&episode_no=116
F12打開開發者工具, 進行重繪網頁
點擊Img
通過對比分析請求url地址變化 —> 漫畫內容都是來于章節鏈接里面
二. 代碼實作步驟程序
1. 發送請求 ---> 對于目錄頁面發送請求
2. 獲取資料 ---> 服務器回傳回應資料 <網頁源代碼資料>
3. 決議資料 ---> 提取想要章節鏈接 / 漫畫名字 / 章節名字
4. 發送請求 ---> 對于章節鏈接發送請求
5. 獲取資料 ---> 服務器回傳回應資料 <網頁源代碼資料>
6. 決議資料 ---> 提取想要圖片鏈接
7. 保存資料 ---> 保存到本地
效果展示
不知不覺都四千多張圖了,大家別一窩蜂去爬了,等下網站崩了就不好了~

代碼展示
發送請求
def 自定義函式關鍵字
get_response: 自定義函式名字
模擬瀏覽器對于url地址發送請求
param html_url: 自定義形式引數
return: 回應物件
def get_response(html_url): # 請求頭 headers 模擬瀏覽器 ---> 字典資料型別, 構建完整鍵值對 <偽裝請求頭可以復制粘貼> headers = { # referer 防盜鏈 告訴服務器請求url地址 是從哪里跳轉過來 'referer': 'https://www.dongmanmanhua.cn/', # User-Agent 瀏覽器基本身份資訊 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36' } # 通過資料請求模塊 去發送請求 response = requests.get(url=html_url, headers=headers) # return 回傳 ---> 在代碼里面 呼叫 get_response 函式 這個函式, 會給我們回傳 response 這個資料 return response
獲取章節鏈接 / 漫畫名字 / 章節名字
def get_info(html_url): # 呼叫發送請求函式 html_data =https://www.cnblogs.com/hahaa/p/ get_response(html_url).text # re正則提取資料 name = re.findall("title_title: '(.*?)',", html_data)[0] # 提取漫畫名字 chapter_url_list = re.findall('data-sc-name="PC_detail-page_related-title-list-item".*?href="https://www.cnblogs.com/hahaa/p/(.*?)"', html_data, re.S) title_list = re.findall('<span ><span>(.*?)</span></span>', html_data) return name, chapter_url_list, title_list
獲取漫畫url地址
def get_img_url(chapter_url): # 呼叫發送請求函式 chapter_data =https://www.cnblogs.com/hahaa/p/ get_response(chapter_url).text # re獲取所有漫畫圖片內容 img_url_list = re.findall('alt="image" data-url="(.*?)"', chapter_data) # 403 Forbidden 沒有訪問權限 ---> 通過代碼得到資料 請求頭里面加防盜鏈 return img_url_list
保存資料
def save(name, title, img_url): """ :param name: 漫畫名 :param title: 圖片名 :param img_url: 圖片鏈接 :return: """ # 自動創建檔案夾 file = f'img\\{name}\\' # 如果沒有這個檔案夾的話 if not os.path.exists(file): # 自動創建檔案夾 os.makedirs(file) # 對于圖片鏈接發送請求 獲取二進制資料 img_content = get_response(img_url).content # file + title 保存地方以及保存檔案名 mode 保存方式 with open(file + title, mode='wb') as f: # 寫入資料 f.write(img_content) print(name, title)
主函式
整合上面所有內容
def main(page): """ 完整原始碼及視頻講解再這個群 279199867 自取即可 :param page: :return: """ # 目錄頁面 link = f'https://www.dongmanmanhua.cn/BOY/moutianchengweimoshen/list?title_no=1519&page={page}' # 呼叫獲取章節鏈接 / 漫畫名字 / 章節名字 函式 name, chapter_url_list, title_list = get_info(link) # for回圈遍歷 提取資料 for chapter_url, chapter_title in zip(chapter_url_list, title_list): # 字串拼接 chapter_url = 'https:' + chapter_url # 獲取漫畫內容 img_url_list = get_img_url(chapter_url) # for回圈遍歷 提取資料 num = 1 for img_url in img_url_list: title = chapter_title + str(num) + '.jpg' # 呼叫保存資料函式 save(name, title, img_url) # 每次回圈 +1 num += 1
函式入口, 當你代碼被當作模塊呼叫的時候, 下面的代碼不執行,
if __name__ == '__main__': for page in range(12, 0, -1): main(page)
好了,今天的分享就到這嘍,完整原始碼及視頻講解下方名片自取即可~
最后分享一套Python教程:Python實戰100例
希望對你有所幫助哈~
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/508767.html
標籤:Python
下一篇:變數與資料型別
