流程

代码

+ python代码
# 爬取次元图片(http://www.ciyuandao.com/photo/list/0-4-1)数据

import requests
import re
import os

# 1. 发送请求
headers = {
    # User-Agent: 用户代理,表示浏览器/设备基本身份信息
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36'
}
link = f'http://www.ciyuandao.com/photo/list/0-4-1'
link_data = requests.get(url = link, headers=headers).text
img_id_list = re.findall('<a href="/photo/show/(\\d+)">', link_data)
# for循环遍历,提取列表中的元素
for img_id in img_id_list:
    # 请求网址
    url = f'http://www.ciyuandao.com/photo/show/{img_id}'
    # 发送请求
    response = requests.get(url, headers=headers)
    # 获取数据
    html = response.text
    # print(html)
    # 解析数据
    # 提取标题
    old_title = re.findall('<title>(.*?) - 次元岛</title>',html)[0]
    # 替换特殊字符
    title = re.sub(r'[\\/:*?"<>|\n]','',old_title)
    print(title)
    # 提取图片链接
    img_url_list = re.findall('<a href="javascript:;"><img src="(.*?)"></a>',html)
    """创建文件夹"""
    # 定义文件路径
    path = f'data\\{title}\\'
    if not os.path.exists(path):
        # 创建文件夹
        os.makedirs(path)

    num = 1
    # for循环遍历,提取列表里面的元素
    for img_url in img_url_list:
        img_content = requests.get(url = img_url, headers=headers).content
        # 保存数据
        with open(path + title + str(num) + '.jpg', 'wb') as f:
            f.write(img_content)
        num += 1
        print(img_url)