dm5爬虫(selenium)

news/2024/7/19 11:49:43 标签: 爬虫, selenium, python

因为纯requests请求获取不到图片链接,所以用了selenium

在这里插入图片描述

python">

import requests 
import os
from lxml import etree
import re
from selenium import webdriver

url = 'https://www.dm5.com/manhua-qiangweishaonv/' # 漫画的url
response = requests.get(url) # 获取响应
print(response) # 打印响应
html = etree.HTML(response.text) # 解析响应的源代码
manga_title = html.xpath('/html/body/div[3]/section/div[2]/div[2]/p[1]/text()') # 获取漫画标题
chapter_title = html.xpath('/html/body/div[4]/div/div[2]/div[1]/div[2]/ul/li/a/text()') # 获取章节标题
chapter_links=html.xpath('/html/body/div[4]/div/div[2]/div[1]/div[2]/ul/li/a/@href') # 获取章节链接
chapter_img_count=html.xpath('/html/body/div[4]/div/div[2]/div[1]/div[2]/ul/li/a/span/text()') # 获取章节图片数量

chapter_img_count = [re.sub(r'\D', '', count) for count in chapter_img_count] # 保留每个元素中的数字
chapter_img_count = [int(count) for count in chapter_img_count] # 将每个元素转换为整数类型
chapter_links = ['https://www.dm5.com' + link for link in chapter_links] # 将前缀添加到每个链接
manga_title = manga_title[0].replace(' ','') # 去除漫画标题中的空格
chapter_title = [title.replace(' ', '') for title in chapter_title]# 去除章节标题列表中每个元素中的空格
chapter_title = list(filter(None, chapter_title))#删除空元素

print(manga_title)
print(chapter_title)
print(chapter_links)
print(chapter_img_count)


chapter_count = 2#len(chapter_links)#结束章节
x=1#开始章节
while x<chapter_count:

    print('链接',chapter_links[x])
    print('章节名',chapter_title[x])

    print('图片数',chapter_img_count[x])
    xx =0
    
    header = {

'referer': chapter_links[x],
'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'image',
'sec-fetch-mode': 'no-cors',
'sec-fetch-site': 'cross-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
}

   
    while xx<chapter_img_count[x]: # 当前章节的图片数量

            chapter_img_url = chapter_links[x]+'#ipg{}'.format(xx) # 当前图片页的url
            print(chapter_img_url) # 打印当前图片页的url
            driver = webdriver.Chrome() # 启动Chrome浏览器
            driver.get(chapter_img_url) # 访问当前图片页的url
            chapter_response = driver.page_source # 获取当前页面的源代码
            chapter_html = etree.HTML(chapter_response) # 解析当前页面的源代码
            img_url = chapter_html.xpath('/html/body/div[6]/div/img/@src') # 获取当前图片的url
            print(img_url) # 打印当前图片的url
            img_response = requests.get(img_url[0],headers=header) # 获取当前图片的响应
        

            if not os.path.exists(f'd:/manga/{manga_title}/{chapter_title[x]}'): # 如果当前章节的文件夹不存在
                os.makedirs(f'd:/manga/{manga_title}/{chapter_title[x]}') # 创建当前章节的文件夹
        

            with open ('d:/manga/%s/%s/%s.jpg'%(manga_title,chapter_title[x],xx),'wb') as f: # 打开当前图片的文件
                f.write(img_response.content) # 写入当前图片的内容
            xx+=1 # 图片数量加1


    x+=1



http://www.niftyadmin.cn/n/284789.html

相关文章

P1915 [NOI2010] 成长快乐

此题为世纪难题 题目提供者 洛谷 难度 NOI/NOI/CTSC 输入输出样例 输入 #1 5 1 6 0 0 1 5 2 2 0 0 输出 #1 1 5 5 2 2 1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~此题非常难&#xff0c;小白就不用想着独自完成了 题解&#xff1a; #…

Linux命令集(Linux文件管理命令--rmdir指令篇)

Linux命令集&#xff08;Linux文件管理命令--rmdir指令篇&#xff09; Linux文件管理命令集&#xff08;rmdir指令篇&#xff09;5. rmdir(remove directory)1. 删除空的目录 folder12. 强制删除目录 folder1&#xff08;包括非空目录&#xff09;3. 递归删除目录及其目录下所有…

地牢大师--(BFS+三维数组)

题目 你现在被困在一个三维地牢中&#xff0c;需要找到最快脱离的出路&#xff01; 地牢由若干个单位立方体组成&#xff0c;其中部分不含岩石障碍可以直接通过&#xff0c;部分包含岩石障碍无法通过。 向北&#xff0c;向南&#xff0c;向东&#xff0c;向西&#xff0c;向上或…

分布式一致性Hash算法原理及实现【负载均衡】

文章目录 一致性Hash原理提高容错性和和扩展性一致性Hash实现思路代码 一致性Hash原理 简单来说&#xff0c;一致性Hash算法将整个哈希值空间组织成一个虚拟的圆环&#xff0c; 如假设某哈希函数 H 的值空间为 0 ~ 2^32-1&#xff08;即哈希值是一个32位无符号整形&#xff09;…

10.watchEffect.下

学习要点&#xff1a; 1.watchEffect 本节课我们来要了解一下 Vue3.x 中的侦听 watchEffect 的用法&#xff1b; 一&#xff0e;watchEffect 1. 在 watchEffect()中的第二参数&#xff0c;我们可以设置一下 options 选项&#xff1b; 2. 首先先理解一下后面将要学的声明周期中的…

《LearnUE——基础指南:上篇—3》——GamePlay架构WorldContext,GameInstance,Engine之间的关系

目录 平行世界是真实存在的吗&#xff1f; 1.3.1 引言 1.3.2 世界管理局&#xff08;WorldContext&#xff09; 1.3.3 司法天神&#xff08;GameInstance&#xff09; 1.3.4 上帝&#xff08;Engine&#xff09; 1.4 总结 平行世界是真实存在的吗&#xff1f; 1.3.1 引言 …

Blender启动场景的修改

Blender启动场景的修改 1 使用版本2 现象描述3 解决方法4 启动场景路径5 清理场景资源5.1 空场景大小5.2 清理图片资源5.2.1 断开数据块关联5.2.2 断开伪用户关联5.2.3 断开多用户关联5.2.4 清理数据块5.2.6 文件校验 5.3 使用自建资源库 6 数据块类型 1 使用版本 Blender 3.3…

深度学习 -- PyTorch学习 torchvision工具学习 Transforms模块 Normalize用法

前言 计算机视觉是深度学习的一个重要应用领域。PyTorch提供现成的torchvision工具&#xff0c;帮助处理图像和视频。torchvision包含一些常用的数据集、模型、转换函数等&#xff0c;学习和使用这些API有助于更快更好的在CV领域应用PyTorch。 torchvision torchvision的数据…