【Selenium爬取小说】

Selenium爬取小说

确定url

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

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

看class 或者 id 后面等于的值的时候 match 不一定是1 但是只要 这个标签下id=的这个值是唯一标识的即可 ,因为你搜索的是全部的整个页面下的这个值 但是class[id=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()

爬取的结果

欢迎批评指正

相关推荐
爬虫程序猿5 小时前
把 1688 商品详情搬进 MySQL:PHP 爬虫全链路实战(2025 版)
爬虫·python·音视频
歪歪10010 小时前
使用 Wireshark 进行 HTTP、MQTT、WebSocket 抓包的详细教程
网络·websocket·测试工具·http·wireshark
一晌小贪欢10 小时前
Python爬虫第7课:多线程与异步爬虫技术
开发语言·爬虫·python·网络爬虫·python爬虫·python3
桃子不淘气21 小时前
3:Django-migrate
测试工具
将车2441 天前
selenium实现自动化脚本的常用函数
python·selenium·自动化
hwman1 天前
使用Selenium Server 4连接已经运行的Firefox
selenium·测试工具·firefox
一百天成为python专家1 天前
python爬虫入门(小白五分钟从入门到精通)
开发语言·爬虫·python·opencv·yolo·计算机视觉·正则表达式
wanfeng_091 天前
python爬虫学习
爬虫·python·学习
濑户川1 天前
基于DDGS实现图片搜索,文本搜索,新闻搜索
人工智能·爬虫·python
卓码软件测评1 天前
第三方课题验收测试机构:【API测试工具Apifox使用指南】
功能测试·测试工具·单元测试·压力测试·可用性测试