Python怎么爬取漫画图片

发布时间:2021-11-25 14:16:06 作者:小新
来源:亿速云 阅读:258

这篇文章主要介绍了Python怎么爬取漫画图片,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

开发环境:

目标地址

https://www.dmzj.com/info/yaoshenji.html

Python怎么爬取漫画图片

代码

导入工具

import requests
import os
import re
from bs4 import BeautifulSoup
from contextlib import closing
from tqdm import tqdm
import time

获取动漫章节链接和章节名

r = requests.get(url=target_url)
bs = BeautifulSoup(r.text, 'lxml')
list_con_li = bs.find('ul', class_="list_con_li")
cartoon_list = list_con_li.find_all('a')
chapter_names = []
chapter_urls = []
for cartoon in cartoon_list:
    href = cartoon.get('href')
    name = cartoon.text
    chapter_names.insert(0, name)
    chapter_urls.insert(0, href)
print(chapter_urls)

下载漫画

for i, url in enumerate(tqdm(chapter_urls)):
    print(i,url)
    download_header = {
        'Referer':url,
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
    }
    name = chapter_names[i]
    # 去掉.
    while '.' in name:
        name = name.replace('.', '')
    chapter_save_dir = os.path.join(save_dir, name)
    if name not in os.listdir(save_dir):
        os.mkdir(chapter_save_dir)
    r = requests.get(url=url)
    html = BeautifulSoup(r.text, 'lxml')
    script_info = html.script
    pics = re.findall('\d{13,14}', str(script_info))
    for j, pic in enumerate(pics):
        if len(pic) == 13:
            pics[j] = pic + '0'
    pics = sorted(pics, key=lambda x: int(x))
    chapterpic_hou = re.findall('\|(\d{5})\|', str(script_info))[0]
    chapterpic_qian = re.findall('\|(\d{4})\|', str(script_info))[0]
    for idx, pic in enumerate(pics):
        if pic[-1] == '0':
            url = 'https://images.dmzj.com/img/chapterpic/' + chapterpic_qian + '/' + chapterpic_hou + '/' + pic[
                                                                                                             :-1] + '.jpg'
        else:
            url = 'https://images.dmzj.com/img/chapterpic/' + chapterpic_qian + '/' + chapterpic_hou + '/' + pic + '.jpg'
        pic_name = '%03d.jpg' % (idx + 1)
        pic_save_path = os.path.join(chapter_save_dir, pic_name)
        print(url)
        response = requests.get(url,headers=download_header)
        # with closing(requests.get(url, headers=download_header, stream=True)) as response:
            # chunk_size = 1024
            # content_size = int(response.headers['content-length'])
        print(response)
        if response.status_code == 200:
            with open(pic_save_path, "wb") as file:
                # for data in response.iter_content(chunk_size=chunk_size):
                    file.write(response.content)
        else:
            print('链接异常')
    time.sleep(2)

创建保存目录

save_dir = '妖神记'
if save_dir not in os.listdir('./'):
    os.mkdir(save_dir)
target_url = "https://www.dmzj.com/info/yaoshenji.html"

感谢你能够认真阅读完这篇文章,希望小编分享的“Python怎么爬取漫画图片”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. python如何爬取图片
  2. python如何爬取ajax

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:Mac环境下如何使用Python Scrapy框架

下一篇:如何理解多路开关模式的switch语句

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》