赞
踩
在QQ看点上看到的一部漫画,正好最近对爬虫比较感兴趣,还没处理过图片的下载,所以想试一下。废话不多说,直接上源码:
import os,base64 import requests import re from PIL import Image from io import BytesIO # 第一话漫画地址 url = "https://rimanb.com/chapter/54717" # 图片编号 i=1 # 得到网页源码 req = requests.get(url) content = req.text # 得到所有话的地址 obj1 = re.compile(r'<a href=\"(?P<url_next>.*?)\" title=\"第') result = obj1.finditer(content) # 访问每一话的网页 for it in result: url = "https://rimanb.com"+it.group("url_next") # 图片链接读取 req1 = requests.get(url) content = req1.text obj2 = re.compile(r'<img src=\"(?P<url_img>.*?)?cid') result2 = obj2.finditer(content) for It in result2: url_img=It.group("url_img").replace("?","") response = requests.get(url_img) # 将这个图片保存在内存 image = Image.open(BytesIO(response.content)) ls_f=base64.b64encode(BytesIO(response.content).read()).decode('utf-8') imgdata=base64.b64decode(ls_f) filename = "漫画/"+str(i)+".jpg" file=open(filename,'wb') file.write(imgdata) i=i+1 print(url)
说一下思路,最重要的先找到一个有这个漫画的网页地址。
我看了网页源码,发现目录地址就在第一话的源码中
<a href="/chapter/54717" title="第1话">
<span class="tool_chapters_list_number">[1]</span>
<span class="tool_chapters_list_title">第1话</span>
</a>
</li>
<li>
<a href="/chapter/54718" title="第2话">
<span class="tool_chapters_list_number">[2]</span>
<span class="tool_chapters_list_title">第2话</span>
</a>
</li>
所以我写了个正则表达式,将所有章节的网址拿出来,挨个章节取图片
obj1 = re.compile(r'<a href=\"(?P<url_next>.*?)\" title=\"第')
result = obj1.finditer(content)
# 访问每一话的网页
for it in result:
#将result中的结果遍历,result中的结果就是每个章节
#章节地址
url = "https://rimanb.com"+it.group("url_next")
下面是漫画网页源码中图片地址的片段,只需要得到http://image.mangabz.com/8/7038/117769/1_4711.jpg,就可以进行下载
<li style="width: 800px; height: auto;">
<img src="http://image.mangabz.com/8/7038/117769/1_4711.jpg?cid=117769&key=2917e5a0a9cbe082695f61522dba251a&type=1&uk=07899030407A56CCF6A9E10D3E95ABB0555146BC3088245462C591EE879A6315\" alt="" class="loaded" style="opacity: 1;">
</li>
根据url,下载图片的 一段代码是借鉴别人的
response = requests.get(url_img) # 将这个图片保存在内存
image = Image.open(BytesIO(response.content))
ls_f=base64.b64encode(BytesIO(response.content).read()).decode('utf-8')
imgdata=base64.b64decode(ls_f)
filename = "漫画/"+str(i)+".jpg"
file=open(filename,'wb')
file.write(imgdata)
以上就是爬取漫画的全过程
第一次写文章,大家有啥建议可以提出来,也可以和我交流交流爬虫
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。