当前位置:   article > 正文

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)
  • 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
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

说一下思路,最重要的先找到一个有这个漫画的网页地址。
我看了网页源码,发现目录地址就在第一话的源码中

<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

所以我写了个正则表达式,将所有章节的网址拿出来,挨个章节取图片

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")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

下面是漫画网页源码中图片地址的片段,只需要得到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>
  • 1
  • 2
  • 3

根据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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/55500
推荐阅读
相关标签
  

闽ICP备14008679号