【Selenium爬取小说】

Selenium爬取小说

确定url

找到你所需要的网站 然后进行分析检查 。

==注意: 进行搜索元素时 会有一个ctrl+f的操作

看class 或者 id 后面等于的值的时候 match 不一定是1 但是只要 这个标签下id=的这个值是唯一标识的即可 ,因为你搜索的是全部的整个页面下的这个值 但是classid=xxx这个会可能是唯一的。

进行分析页面在爬取

可以发现都在dd标签下

多层爬取 进入这个页面 然后爬取这一章的内容

可以发现内容都在这个标签下

我们打开一个文件接受这个文本即可

爬虫代码

python 复制代码
import time
from selenium import webdriver
from selenium.webdriver.common.by import By

file = open('./output/xxx/明克街13号.txt','a',encoding= 'utf-8')
driver = webdriver.Firefox()
url = 'https://www.xxxxx.bz/book/54529/'
driver.get(url)
dd_list =driver.find_elements(By.XPATH,"//div[@id='list']/dl/dd")
print(dd_list)
number = 1
for i in range(12,len(dd_list)):
    print(f'爬取第{number}章')
    detail_url = dd_list[i].find_element(By.XPATH,'a').get_attribute('href')
    print(detail_url)
    driver_chmo = webdriver.Chrome()
    driver_chmo.get(detail_url)
    response = driver_chmo.find_element(By.XPATH,'//div[@id="content"]')
    print(response.text)
    file.write(response.text+f'\n   第{number}章   \n')
    number = number + 1
    time.sleep(3)
file.close()

爬取的结果

欢迎批评指正

相关推荐
沙漠之主12 小时前
Python 教学设计资料:从入门到实战的完整课程方案
爬虫·python
深蓝电商API21 小时前
用YOLO训练验证码目标检测模型的全流程
爬虫·yolo·目标检测·目标跟踪
ysu_03141 天前
Postman接口调试实战指南-从HTTP请求到环境变量-Collection与自动化测试
网络·网络协议·测试工具·http·api·postman·可用性测试
wuyk5552 天前
Python网络爬虫入门到实战 第02章:HTTP/HTTPS协议超通俗精讲(GET/POST、请求头、响应码、爬虫核心基础)
爬虫·python·http
szial2 天前
网络爬虫与 CDP 实战(二):浏览器能访问,Python 却被拒绝?拆开登录态与 CSRF
爬虫·python·csrf
ZhangJun953 天前
从零写一个类鲁大师的硬件检测小工具,软件附上,可直接使用
windows·测试工具·电脑·笔记本电脑·硬件架构
梅雅达编程笔记3 天前
04-Python CSV数据保存与翻页抓取
开发语言·爬虫·python·pandas·数据采集·csv
梅雅达编程笔记3 天前
03_网页表格数据抓取
爬虫·python·beautifulsoup·pandas·数据采集
傻啦嘿哟3 天前
爬虫代理IP池从0到1:构建高可用代理池,彻底解决IP被封问题
网络·爬虫·tcp/ip
szial3 天前
网络爬虫与 CDP 实战(一):网页数据到底在哪?从 HTTP 到分页采集
网络·爬虫·python·网络爬虫