40 lines of code for the elementary school girl to collect her favorite comics, so handsome~

Why collect comics?

The new lady from the company turned out to be a primary school girl,
but she likes to read comics very much, and introduces me to me every day, so annoying~

insert image description here

It is now 16:30 on September 17, 2022,

So I decided to write a code before getting off work at five o'clock to get all the comics she said, and deal with it~

insert image description here

Preparations

environmental use

Python 3.8
Pycharm 2021.2版本

module usage

import requests >>> # 数据请求模块  pip install requests
import re  # 正则模块
import os   #  文件操作模块

Basic process ideas

1. Data source analysis

1. 确定自己需求:
    获取什么数据内容

正常访问流程:
    1. 选中漫画 ---> 目录页面 <请求列表页面 获取所有章节链接>
    2. 选择一个漫画内容 ---> 漫画页面 <请求章节链接, 获取所有漫画内容url>
    3. 看漫画内容 <保存数据, 漫画图片内容保存下来>

Analysis process: <Developer tools for packet capture analysis>

    1. 查看漫画图片url地址, 是什么样子

    2. 分析url地址在哪里
        通过搜索功能 <开发者工具> 

F12 open the developer tool, refresh the webpage,
click on Img
to request the url address change through comparative analysis —> the comic content comes from the chapter link


2. Code implementation steps process

1. 发送请求 ---> 对于目录页面发送请求
2. 获取数据 ---> 服务器返回响应数据 <网页源代码数据>
3. 解析数据 ---> 提取想要章节链接 / 漫画名字 / 章节名字

4. 发送请求 ---> 对于章节链接发送请求
5. 获取数据 ---> 服务器返回响应数据 <网页源代码数据>
6. 解析数据 ---> 提取想要图片链接

7. 保存数据 ---> 保存到本地

Show results

Before I knew it, there were more than 4,000 pictures.

Don't act in a hurry, everyone.

It's not good to wait until it collapses~

insert image description here
Please add image description

Code display

send request

def custom function keyword
get_response: custom function name

Simulate the browser to send a request to the url address
param html_url: custom formal parameter
return: response object

def get_response(html_url):
    # 请求头 headers 模拟浏览器 ---> 字典数据类型, 构建完整键值对 <伪装请求头可以复制粘贴>
    headers = {
    
    
        # referer 防盗链 告诉服务器请求url地址 是从哪里跳转过来
        'referer': 'https://www.****.cn/',
        # User-Agent  浏览器基本身份信息
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
    }
    # 通过数据请求模块 去发送请求
    response = requests.get(url=html_url, headers=headers)
    # return 返回 ---> 在代码里面 调用 get_response  函数 这个函数, 会给我们返回 response 这个数据
    return response

Get chapter link/manga name/chapter name

def get_info(html_url):
    # 调用发送请求函数
    html_data = get_response(html_url).text
    # re正则提取数据
    name = re.findall("title_title: '(.*?)',", html_data)[0]  # 提取漫画名字
    chapter_url_list = re.findall('data-sc-name="PC_detail-page_related-title-list-item".*?href="(.*?)"', html_data, re.S)
    title_list = re.findall('<span class="subj"><span>(.*?)</span></span>', html_data)
    return name, chapter_url_list, title_list

Get the comic url address

def get_img_url(chapter_url):
    # 调用发送请求函数
    chapter_data = get_response(chapter_url).text
    # re获取所有漫画图片内容
    img_url_list = re.findall('alt="image" class="_images _centerImg" data-url="(.*?)"', chapter_data)
    # 403 Forbidden 没有访问权限  ---> 通过代码得到数据 请求头里面加防盗链
    return img_url_list

save data

def save(name, title, img_url):
    """
    :param name: 漫画名
    :param title: 图片名
    :param img_url: 图片链接
    :return:
    """
    # 自动创建文件夹
    file = f'img\\{
      
      name}\\'
    # 如果没有这个文件夹的话
    if not os.path.exists(file):
        # 自动创建文件夹
        os.makedirs(file)
    # 对于图片链接发送请求 获取二进制数据
    img_content = get_response(img_url).content
    # file + title  保存地方以及保存文件名 mode 保存方式
    with open(file + title, mode='wb') as f:
        # 写入数据
        f.write(img_content)
    print(name, title)

main function

Integrate all of the above

def main(page):

    # 目录页面
    link = f'https://地址我删了,会被屏蔽.cn/BOY/moutianchengweimoshen/list?title_no=1519&page={
      
      page}'
    # 调用获取章节链接 / 漫画名字 / 章节名字 函数
    name, chapter_url_list, title_list = get_info(link)
    # for循环遍历 提取数据
    for chapter_url, chapter_title in zip(chapter_url_list, title_list):
        # 字符串拼接
        chapter_url = 'https:' + chapter_url
        # 获取漫画内容
        img_url_list = get_img_url(chapter_url)
        # for循环遍历 提取数据
        num = 1
        for img_url in img_url_list:
            title = chapter_title + str(num) + '.jpg'
            # 调用保存数据函数
            save(name, title, img_url)
            # 每次循环 +1
            num += 1

Function entry, when your code is called as a module, the following code is not executed.

if __name__ == '__main__':
    for page in range(12, 0, -1):
        main(page)

Alright, that's it for today's sharing~~

The complete source code and video explain the business card below and you can pick it up yourself~

I'm Red Panda, see you in the next article (✿◡‿◡)

insert image description here

Guess you like

Origin blog.csdn.net/m0_67575344/article/details/126906510