打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python:谁还能抵挡我的脚步,VIP也抵挡不住啦~

前言

嗨喽!大家好呀,这里是魔王~**

知识点:

  1. 爬虫基本流程

  2. parsel数据解析模块的简单使用

  3. os文件操作的简单使用

  4. 拼接图片

开发环境:

  • Python 3.8 解释器

  • Pycharm

模块使用:

  • requests >>> pip install requests 发送请求

  • parsel >>> pip install parsel 解析数据

  • os 文件操作 内置模块

爬虫基本流程: 发送请求(请求页面) 获取数据 解析数据 保存数据 实现步骤

一. 分析数据来源

  • 找请求页面或者请求url地址是什么 / 想要数据内容在哪里可以获取

  • 通过开发者工具抓包分析, 我们想要图片url地址来自于哪里

      1. 找到漫画对应图片url >>> 图片url的数据包是什么(会有数据包专门存储所有图片url的)  2. 找到图片的数据包url之后(仅仅一话的内容)

通过分析对比可得, 只要获取所有漫画章节ID就可以获取所有漫画内容....
ID类参数, 都可以去列表页面获取(目录列表)

想要获取所有漫画内容

  1. 获取得到所有漫画章节ID

  2. 把章节ID传入图片数据包里面

  3. 获取所有图片url地址

二. 代码实现步骤:

  1. 发送请求, 对于漫画目录页面发送请求

  2. 获取数据, 获取服务器返回响应数据内容

  3. 解析数据, 提取我们想要漫画ID以及漫画标题名字

  4. 发送请求, 把ID传入图片数据包里面发送请求

  5. 获取数据, 获取服务器返回响应数据内容

  6. 解析数据, 提取我们想要图片url

  7. 保存数据....

代码

爬取漫画

import requestsimport parsel  # 解析数据模块 没有办法直接对于字符串数据进行提取import os
# 1. 发送请求, 对于漫画目录页面发送请求
url = 'https://www.mkzhan.com/216191/' # 确定请求url地址
headers = { 'cookie': '__login_his_sync=0; UM_distinctid=18007d5fd5f75-0dd953d0f62cee-1a343370-1fa400-18007d5fd60753; CNZZDATA1262045698=1849049383-1649398645-%7C1649398645; tourist_expires=1; readMode=scroll; CNZZDATA1261814609=1771138736-1649393424-https%253A%252F%252Fwww.mkzhan.com%252F%7C1649393279; redirect_url=%2F216191%2F; cn_1262045698_dplus=%7B%22distinct_id%22%3A%20%2218007d5fd5f75-0dd953d0f62cee-1a343370-1fa400-18007d5fd60753%22%2C%22%24_sessionid%22%3A%200%2C%22%24_sessionTime%22%3A%201649400570%2C%22%24dp%22%3A%200%2C%22%24_sessionPVTime%22%3A%201649400570%7D', 'referer': 'https://www.mkzhan.com/category/?order=2&is_vip=1', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36',
}
response = requests.get(url=url, headers=headers) # <Response [200]> 200表示请求
# 2. 获取数据
# print(response.text) # response.text 得到html字符串的数据内容
# 3. 解析数据,提取章节ID以及章节名字
selector = parsel.Selector(response.text) # 为什么要写这行代码 把html数据内容转成可解析对象 selector
# css选择器, 就是根据标签属性内容提取数据 分开两次提取 第一次提取 提取所有li标签
lis = selector.css('.chapter__list-box li')for li in list(reversed(lis))[1:]:
chapter_id = li.css('a::attr(data-chapterid)').get()
# strip() 去除字符串[左右]两端的空格
chapter_title = li.css('a::text').getall()[-1].strip() print(chapter_id, chapter_title)
filename = f'{chapter_title}\\'
if not os.path.exists(filename):
os.mkdir(filename)
link = 'https://comic.mkzcdn.com/chapter/content/v1/'
data = { 'chapter_id': chapter_id, 'comic_id': '216191', 'format': '1', 'quality': '1', 'sign': 'bcd29b22b3ab8e7baa284ed431975a95', 'type': '1', 'uid': '53013317',
}
json_data = requests.get(url=link, params=data, headers=headers).json()['data']['page']
page = 1
for index in json_data:
img_url = index['image'] print(img_url)
img_content = requests.get(url=img_url, headers=headers).content
with open(filename + str(page) + '.jpg', mode='wb') as f:
f.write(img_content)
page += 1

解答、资料、源码点击领取~


在这里插入图片描述

长图

from PIL import Image
from os import listdir'''result_path_target 图片存放目标路径
cut_pictures 待拼接图片存放路径
num 图片命名以数字按序增加'''def get_picture(cut_pictures, title):
# cut_pictures = r'D:\自游\漫画\第1话 重生'
ims = [Image.open(cut_pictures + '\\' + fn) for fn in listdir(cut_pictures) if fn.endswith(".jpg")] # 打开路径下的所有图片
width, height = ims[0].size # 获取拼接图片的宽和高
result = Image.new(ims[0].mode, (width, height * len(ims)))
for j, im in enumerate(ims):
result.paste(im, box=(0, j * height))
result.save(cut_pictures + '%s.jpg' % title)



get_picture('第1话上 三个相宫\\', '第1话上 三个相宫')


在这里插入图片描述

图片查看器

import tkinter as tkimport glob

from PIL import Image, ImageTk

current_photo_no = 0root = tk.Tk()
# 加载本地图片
photos = glob.glob('第1话上 三个相宫/*.jpg')
photos = [ImageTk.PhotoImage(Image.open(file)) for file in photos]"""在下面实现代码"""root.title("图片浏览器")
root.geometry('1024x1080+100+100')

photo_label = tk.Label(root, image=photos[current_photo_no], width=800, height=1249)
photo_label.pack()

number_var = tk.StringVar()
number_var.set(' 1 of 4')
tk.Label(root, textvariable=number_var, bd=1, relief=tk.SUNKEN, anchor=tk.CENTER).pack(fill=tk.X)

button_frame = tk.Frame(root)
button_frame.pack()

# 布局到页面上
prev_photo = tk.Button(button_frame, text="上一页")
next_photo = tk.Button(button_frame, text="下一页")
# 根据键盘 点击一下
prev_photo.pack(side=tk.LEFT, anchor=tk.CENTER)
next_photo.pack(side=tk.RIGHT, anchor=tk.CENTER)


def _calculation_photo_no(next_no):
global current_photo_no
current_photo_no += next_no

# 如果序号小于 0,就设置为最后一张 if current_photo_no < 0:
current_photo_no = len(photos) - 1
# 如果序号大于最大长度,就修改为序号 0
if current_photo_no == len(photos):
current_photo_no = 0

# 设置当前图片的序号信息
number_var.set(f'{current_photo_no + 1} of {len(photos)}')
# 修改显示的图片内容
photo_label.configure(image=photos[current_photo_no])


# 绑定点击事件
prev_photo.configure(command=lambda: _calculation_photo_no(-1))
next_photo.configure(command=lambda: _calculation_photo_no(1))

root.mainloop()

视频教程

https://www.bilibili.com/video/BV1hS4y1u7k5?share_source=copy_web

尾语

好了,我的这篇文章写到这里就结束啦!


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
python采集漫客栈所有数据,从此看漫不用money
Python爬虫入门教程02:笔趣阁小说爬取
利用Python爬虫技术的一些骚操作
Python爬虫实战案例:取喜马拉雅音频数据详解
Python爬虫入门教程:豆瓣Top电影爬取
如何用python爬虫从爬取一章小说到爬取全站小说
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服