解锁VIP会员漫画:用Python爬虫轻松实现高清漫画下载
嗨喽~大家好呀,这里是魔王呐 ❤ ~!
python更多源码/资料/解答/教程等 点击此处跳转文末名片免费获取
环境使用:
-
Python 3.10
-
Pycharm
模块使用:
-
requests >>> pip install requests 数据请求模块
-
parsel >>> pip install parsel 数据解析模块
模块安装:
win + R 输入cmd 输入安装命令 pip install 模块名
例如: requests >>> pip install requests
爬虫实现的基本流程
一. 数据来源分析
-
明确需求
明确采集的网站以及数据内容
-
网址: https://www.mkzhan.com/209412/1004107.html
-
数据: 漫画内容(41张图片)
-
-
抓包分析 (浏览器中进行操作)
通过浏览器自带开发者工具, 分析我们需要的数据内容在什么地方
-
开发开发者工具: F12 / 右键点击检查选择 network (网络)
-
刷新网页: 让网页数据内容重新加载一遍
-
找到图片链接: 过滤图片直接点击Img
-
通过关键字找到对应数据包: 存在一个数据包含了整章漫画内容数据 (41张图)
关键字: 使用图片链接中一段参数即可
-
数据包地址:
https://comic.mkzcdn.com/chapter/content/v1/?chapter_id=1004107&comic_id=209412&format=1&quality=1&sign=80cc6ea2ef3e7911cdaef9199d74c66a&type=1&uid=69982021
二. 代码实现步骤
-
发送请求
模拟浏览器对于url地址发送请求
-
模拟浏览器 (可以直接复制)
-
请求网址:
https://comic.mkzcdn.com/chapter/content/v1/?chapter_id=1004107&comic_id=209412&format=1&quality=1&sign=80cc6ea2ef3e7911cdaef9199d74c66a&type=1&uid=69982021
-
发送请求: requests模块 根据开发者工具提示请求方法去发送即可
-
代码内容
导入模块
'''
Python学习交流,免费公开课,免费资料,
免费答疑,系统学习加QQ群:926207505
'''
import requests
import os
import parsel
import re
“”"
发送请求函数
“”"
def GetResponse(url):# 模拟浏览器 (伪装)headers = {# User-Agent 用户代理, 表示浏览器基本身份信息'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36'}# 发送请求response = requests.get(url=url, headers=headers)# 返回值return response
“”"
获取图片链接
“”"
'''
Python学习交流,免费公开课,免费资料,
免费答疑,系统学习加QQ群:926207505
'''
def GetImg(ID):# 请求网址url = f'https://comic.mkzcdn.com/chapter/content/v1/?chapter_id={ID}&comic_id=209412&format=1&quality=1&sign=80cc6ea2ef3e7911cdaef9199d74c66a&type=1&uid=69982021'# 发送请求response = GetResponse(url=url)# 获取数据内容JsonData = response.json()# 解析数据 1. 提取图片链接所在列表 字典取值 (基础语法)pages = JsonData['data']['page']"""# 创建空列表ImgList = []# 2. 提取列表里面元素, 并且提取图片链接for page in pages:# 提取图片链接img = page['image']# 把图片链接添加到 ImgList 列表里面ImgList.append(img)"""# 列表推导式ImgList = [page['image'] for page in pages]# 返回内容return ImgList
“”"
保存数据函数
“”"
def Save(img, title):# 发送请求 + 获取数据内容ImgContent = GetResponse(url=img).content# 程序自动创建文件夹if not os.path.exists('img'): # 判断如果没有# 自动创建文件夹os.mkdir('img')# 指定了保存文件夹 -> imgwith open(f'img\\{title}.jpg', mode='wb') as f:f.write(ImgContent)
“”"
获取漫画信息: 名字 / 章节名 / 章节ID
“”"
'''
Python学习交流,免费公开课,免费资料,
免费答疑,系统学习加QQ群:926207505
'''
def GetInfo():# 请求网址link = 'https://www.mkzhan.com/209412/'# 发送请求 + 获取数据HtmlData = GetResponse(url=link).text# 解析数据selector = parsel.Selector(HtmlData)# 提取名字name = selector.css('.de-info__box .comic-title::text').get()# 提取章节名 + 章节ID所在li标签 (所有)lis = selector.css('.chapter__list .chapter__list-box .chapter__item')# 创建空列表TitleList = []ChapterIdList = []# for循环遍历, 二次提取for li in lis:# 提取章节名字title = li.css('a::text').getall()[-1].strip()# 提取章节IDchapter_id = li.css('a::attr(data-chapterid)').get()TitleList.append(title)ChapterIdList.append(chapter_id)return name, TitleList, ChapterIdListdef main():# 获取漫画信息name, TitleList, ChapterIdList = GetInfo()for old_title, chapter_id in reversed(list(zip(TitleList, ChapterIdList))):# 定义函数需要调用函数ImgList = GetImg(ID=chapter_id) # 返回图片列表print('正在保存: ', old_title)# 定义标题变量num = 1# 替换特殊字符title = re.sub(r'[\\/:*?"<>|]', '', old_title)for img in ImgList:ImgName = f'{title}-{num}'# 调用保存函数Save(img=img, title=ImgName)num += 1if __name__ == '__main__':main()
尾语
最后感谢你观看我的文章呐~本次航班到这里就结束啦 🛬
希望本篇文章有对你带来帮助 🎉,有学习到一点知识~
躲起来的星星🍥也在努力发光,你也要努力加油(让我们一起努力叭)。