原来制作一个漫画网站这么简单!!!!!
制作一个漫画网站的流程是什么?
获取漫画数据
第一步必然是要获取到漫画数据,漫画平台可以说数不胜数,腾讯漫画,漫画台,咚漫漫画等等,所以我们要获取数据必然也是从这些平台去获取,这里就随便从一个漫画平台去分析获取漫画数据.
爬取网站四部曲
分析
我们以爬取比较热门的动漫--->妖神记
我们先随便进入一话
先打开控制台,第一时间查看请求,先判断我们需要的数据是通过Fetch/XHR还是文档直接生成的,通过这样去判断我们写脚本的方式是直接通过获取数据还是用xml去定位数据并获取
查看这个请求里面的参数和返回结果,寻找是否有我们想要的数据
用postman直接测试接口,查看是否需要cookie或者其他的header
可以看到并不需要任何其他条件(这样就简单了)
问题
参数有哪些变化,我们可以对比第一话和第二话的请求参数
很明显主要差别在于chapter_newid,那这个参数怎么获得,我们其实看看第一话和第二话的请求结果就会发现到这个参数
所以我们可以通过第一话就直接获得下一话参数,这样我们就可以开始写代码了
代码实现
import os
import requests
from PIL import Image
from io import BytesIO
def MergeImage(width, height, imgList, save_img):
# -----create a new image-----#
img = Image.new("RGB", (width, height), (0, 0, 0))
h = 0
for iimg in imgList:
img.paste(iimg, (0, h))
h += iimg.size[1]
img.save(save_img)
print("保存成功")
def saveComic(splitComic):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.53",
"Referer": "https://www.manhuatai.com/",
"Accept": "image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
}
chapter_newid = "dyhzs"
while True:
params = {
"product_id": 2,
"productname": "mht",
"platformname": "pc",
"comic_id": 27417,
"chapter_newid": chapter_newid,
"isWebp": 1,
"quality": "middle"
}
url = "https://www.manhuatai.com/api/getchapterinfov2"
# 请求漫画api
resp = requests.get(url=url, params=params, headers=headers).json()
# 获取标题
chapter_name = resp['data']['current_chapter']['chapter_name']
# 获取当前话的图片列表
chapter_img_list = resp['data']['current_chapter']['chapter_img_list']
# 获取目录名
dirName = f"./result/{resp['data']['comic_name']}"
# 判断目录是否存在,不存在则创建目录
if not os.path.exists(dirName):
os.mkdir(dirName)
index = 1
imgList = []
maxWidth = 0
height = 0
for imgUrl in chapter_img_list:
content = requests.get(url=imgUrl, headers=headers).content
if not splitComic:
# 直接将分开的图片合并在一起
bytes_stream = BytesIO(content)
image = Image.open(bytes_stream)
imgList.append(image)
maxWidth = max(maxWidth, image.size[0])
height += image.size[1]
else:
# 判断目录是否存在,不存在则创建目录
if not os.path.exists(dirName+"/"+chapter_name):
os.mkdir(dirName+"/"+chapter_name)
with open(f"{dirName}/{chapter_name}/{chapter_name}{index}.webp",mode="wb") as fs:
fs.write(content)
index += 1
if not splitComic:
MergeImage(maxWidth, height, imgList, f"{dirName}/{chapter_name}.png")
# 最后一话直接跳出
if resp['data']['next_chapter']:
# 获取下一话的chapter_newid
chapter_newid = resp['data']['next_chapter']['chapter_newid']
next_chapter_name = resp['data']['next_chapter']['chapter_name']
print(f"{chapter_name}已经下载完成,即将下载{next_chapter_name},chapter_newid为{chapter_newid}")
else:
break
if __name__ == '__main__':
saveComic(False)