python爬取漫画

python爬取漫画

在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&amp;key=2917e5a0a9cbe082695f61522dba251a&amp;type=1&amp;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)

以上就是爬取漫画的全过程

第一次写文章,大家有啥建议可以提出来,也可以和我交流交流爬虫

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老衲要还俗娶婆娘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值