最近想找点漫画看,发现芝士漫画这个平台不错,于是就想着下载来看。
安装 python
下载 python
下载好 python 安装包,安装时一路 next 过去,注意勾上“add to path”(一般是默认勾选的)。
安装所需的模块
本次的爬虫使用 requests 模块获取数据,再用 xpath 语法解析代码。
需要的模块包括 requests,lxml,reportlab。
使用 pip 安装
1 | pip install requests |
写代码
- 新建一个 python 文件
- 导入相应的模块
1 | import requests |
- 定义全局变量
1 | headers = { |
定义方法
- get_text():得到访问返回的资源
1
2
3def get_text(url):
response = requests.get(url, headers=headers)
return response- parse_text(text):分析文档,保存图片,并得到下一章的 url
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21def parse_text(text):
# 建立解析
html = etree.HTML(text, etree.HTMLParser())
# 获取漫画图片所有的链接
links = html.xpath("//div[@id='gallery-1']//dt/a/@href")
for i, link in enumerate(links):
save_image(link, i)
# 降低访问的速度,防止被识别为机器人
time.sleep(15)
print("完成", i, "个......")
print("已完成", chapter, "的下载!")
# 在第一章的时候只有一个next_chapter链接
try:
next_chapter = html.xpath(
"//div[@class='fusion-single-navigation-wrapper']/a/@href")[1]
except Exception as e:
next_chapter = html.xpath(
"//div[@class='fusion-single-navigation-wrapper']/a/@href")[0]
return next_chapter
return next_chapter- save_image(link, index): 访问得到的图片链接,并保存在本地
1
2
3
4
5
6
7
8
9
10
11def save_image(link, index):
read = requests.get(link)
# 当前文件夹的加上章节名形成新的文件夹
dir_ = current_dir + '\\' + chapter
# 创建文件夹
if not os.path.exists(dir_):
os.makedirs(dir_)
# 保存图片
with open(dir_ + '\\{}'.format(str(index)+".jpg"), 'wb') as fp:
fp.write(read.content)
fp.close()- convert_images_to_pdf(img_path, pdf_path):根据图片的文件夹以及 pdf 文件夹加上文件名来生成 pdf 文件
1
2
3
4
5
6
7
8
9
10
11
12def convert_images_to_pdf(img_path, pdf_path):
pages = 0
(w, h) = portrait(A4)
c = canvas.Canvas(pdf_path, pagesize=portrait(A4))
l = os.listdir(img_path)
l.sort(key=lambda x: int(x[:-4]))
for i in l:
f = img_path + os.sep + str(i)
c.drawImage(f, 0, 0, w, h)
c.showPage()
pages = pages + 1
c.save()- begin():开始,并更新全局变量名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18def begin():
global url
global re_object
global chapter
local_url = input(
"输入要下载的章节url(例: https://manhua.zsh8.com/pxtt/pxtt-041/93181.html)\n>>>")
# 判断是否符合url格式
re_object = re.match(
"https://manhua.zsh8.com/.*?/(.*?)/.*?html", local_url)
if (re_object):
url = local_url
# 更新章节名
re_object = re.match(
"https://manhua.zsh8.com/.*?/(.*?)/.*?html", url)
chapter = re_object.group(1)
print("即将开始.....")
else:
print("输入错误!将使用默认链接!\n")- main():主函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28if __name__ == "__main__":
begin()
boolean = "y"
count = 0
# 保存pdf的文件夹
if not os.path.exists(current_dir + '\\pdf\\'):
os.makedirs(current_dir + '\\pdf\\')
while(boolean == "y"):
text = get_text(url).text
next_chapter = parse_text(text)
img_path = current_dir + '\\' + chapter
pdf_path = current_dir + '\\pdf\\' + chapter + '.pdf'
# 有些图片是错误,会导致无法生成pdf文件。
try:
convert_images_to_pdf(img_path, pdf_path)
except Exception as e:
print(e)
re_object = re.match(
"https://manhua.zsh8.com/.*?/(.*?)/.*?html", next_chapter)
chapter = re_object.group(1)
print("下一章节:", chapter)
count += 1
print(count)
# 下载章节的数目,可根据自己需要更改
if (count > 37):
break
url = next_chapter
运行
直接在终端运行 python 文件,等待一段时间,就会在 python 文件的当前目录下生成文件。
文件夹里的图片:
pdf 文件:
完整代码可参考我的github 库




















