• 用Python下载漫画,每天掌握一个实用知识


    嗨嗨,大家好

    最近看小说和漫画看的有点上头…

    就顺便用Python来采集一下漫画吧

    随便抓个漫画分享一下,搞清楚思路后,自己多练练就能自己采集想看的漫画咯

    请添加图片描述

    知识点:

    • 爬虫基本流程
    • 保存海量漫画数据
    • requests的使用
    • base64解码

    开发环境:

    • 版 本:python 3.8
    • 编辑器:pycharm
    • requests: pip install requests
    • parsel: pip install parsel

    如何安装python第三方模块:

    1. win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install requests)
      回车
    2. 在pycharm中点击Terminal(终端) 输入安装命令

    实现代码:

    1. 发送请求
    2. 获取数据
    3. 解析数据
    4. 保存数据

    代码

    import base64
    import requests
    import re
    import json
    import parsel
    import os
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    # 伪装
    headers = {
        # 用户信息
        'cookie': '__AC__=1; tvf....
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    源码.资料.素材.点击领取即可

    select = parsel.Selector(requests.get(main_url, headers=headers).text)
    title_list = select.css('.chapter-page-all.works-chapter-list li a::text').getall()
    link_list = select.css('.chapter-page-all.works-chapter-list li a::attr(href)').getall()
    
    for title, link in zip(title_list, link_list):
        url = 'https://ac.qq.com' + link
        title = title.strip()
        if not os.path.exists(f'中国惊奇先生/{title}'):
            os.makedirs(f'中国惊奇先生/{title}')
        # 1. 发送请求
        response = requests.get(url=url, headers=headers)
        print(title, url)
        # 2. 获取数据
        html_data = response.text
        # 3. 解析数据
        DATA = re.findall("var DATA = '(.*?)'", html_data)[0]
        for i in range(len(DATA)):
            try:
                json_str = base64.b64decode(DATA[i:].encode("utf-8")).decode("utf-8")
                json_str = re.findall('"picture":(\[.*?\])', json_str)[0]
                # 字符串 转 字典/列表
                json_list = json.loads(json_str)
                count = 1
                for imgInfo in json_list:
                    imgUrl = imgInfo['url']
                    print(imgUrl)
                    # 4. 保存数据
                    img_data = requests.get(url=imgUrl).content
                    with open(f'中国惊奇先生/{title}/{count}.jpg', mode='wb') as f:
                        f.write(img_data)
                    count += 1
                break
            except:
                pass
    
    
    • 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

    请添加图片描述

    最后效果

    在这里插入图片描述

    在这里插入图片描述

    好啦,文章分享到这里也就结束啦

    想用视频学习Python的可以点击此处~

    或者在小破站搜索:Python小圆

    对文章有问题的,或者有其他关于python的问题,可以在评论区留言或者私信我哦
    觉得我分享的文章不错的话,可以关注一下我,或者给文章点赞(/≧▽≦)/

    请添加图片描述

  • 相关阅读:
    ORACLE数据库查询所有索引的sql语句
    老版本Typora强制付费!试试这款开源替代品
    filebeat新filestream类型是否支持tail_files类似功能探究
    Rust中的枚举和模式匹配
    nodejs基于微信小程序的书籍销售系统--(ssm+uinapp+Mysql)
    今天运气不错
    前端实现文件下载的方法
    EF Core从TPH迁移到TPT
    个税计算、税基的处理
    【HTML5入门指北】第一篇 初始HTML5
  • 原文地址:https://blog.csdn.net/aliYz/article/details/127668518