【Python爬虫】使用python脚本拉取网页指定小说章节

示例代码说明:

在小说网站选定一本小说,将小说每个章节内容存为txt文档,文件标题与小说章节标题一致

复制代码
import requests
from lxml import etree
#一本小说链接
Anovellink = 'https://www.hongxiu.com/book/18899519001291804#Catalog'
#目录页代码
ContentsPageCode = requests.get(Anovellink).text
#目录页
ContentsPage = etree.HTML(ContentsPageCode)
href = ContentsPage.xpath('//*[@id="j-catalogWrap"]/div[2]/div/ul/li/a/@href')
for link in href:
    #链接地址
    linkaddress = 'https://www.hongxiu.com' + link
    #章节页面代码
    Chapterpagecode=requests.get(linkaddress).text
    #章节页面
    Chapterpage = etree.HTML(Chapterpagecode)
    #文字列表
    Literallist =Chapterpage.xpath('//div[@class="ywskythunderfont"]/p/text()')
    #标题
    title=Chapterpage.xpath('//h1[@class ="j_chapterName"]/text()')[0]
    file =open('E:/novelpython/'+title+ '.txt','w',encoding='utf-8')
    for paragraph in Literallist:
        file.write(paragraph + '\n')
    print(title +' Chapter crawling is complete')
print('The novel pulling is complete')

结果示例:

相关推荐
郝学胜-神的一滴2 分钟前
深度拆解Python迭代协议:从底层原理到核心实践,解锁异步编程的基石
java·网络·python
2401_833197739 分钟前
更优雅的测试:Pytest框架入门
jvm·数据库·python
weixin_3077791321 分钟前
2025年中国研究生数学建模竞赛A题:通用神经网络处理器下的核内调度问题——解决方案与实现
开发语言·人工智能·python·数学建模·性能优化
2501_9249526923 分钟前
Python深度学习入门:TensorFlow 2.0/Keras实战
jvm·数据库·python
2401_8914821726 分钟前
Python多线程与多进程:如何选择?(GIL全局解释器锁详解)
jvm·数据库·python
2401_8512729926 分钟前
用Python实现自动化的Web测试(Selenium)
jvm·数据库·python
小陈的进阶之路31 分钟前
requests接口自动化测试
python
Yuer202532 分钟前
EDCA OS体系下的:QAS v0.1 实验框架(Python 版)
python·ai量化·edca os
一招定胜负37 分钟前
【实战】Python + 讯飞语音识别 + 通义千问:课堂视频自动转结构化教学数据
python·音视频·语音识别