dm5爬虫(selenium)

该代码示例展示了如何通过结合requests、lxml和selenium库来抓取DM5网站上的漫画页面信息,包括漫画标题、章节标题、章节链接以及图片数量。当requests无法获取图片链接时,selenium被用来模拟浏览器行为,成功获取图片URL并下载。
摘要由CSDN通过智能技术生成

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

在这里插入图片描述



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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值