【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')

结果示例:

相关推荐
AI小怪兽18 分钟前
基于YOLOv13的汽车零件分割系统(Python源码+数据集+Pyside6界面)
开发语言·python·yolo·无人机
wszy180929 分钟前
新文章标签:让用户一眼发现最新内容
java·python·harmonyos
Eric.Lee202139 分钟前
python实现 mp4转gif文件
开发语言·python·手势识别·手势交互·手势建模·xr混合现实
EntyIU42 分钟前
python开发中虚拟环境配置
开发语言·python
wszy18091 小时前
顶部标题栏的设计与实现:让用户知道自己在哪
java·python·react native·harmonyos
kaizq1 小时前
AI-MCP-SQLite-SSE本地服务及CherryStudio便捷应用
python·sqlite·llm·sse·mcp·cherry studio·fastmcp
!chen3 小时前
Error: error:0308010C:digital envelope routines::unsupporte
python
小北方城市网3 小时前
分布式锁实战指南:从选型到落地,避开 90% 的坑
java·数据库·redis·分布式·python·缓存
xiaolyuh1233 小时前
【XXL-JOB】 GLUE模式 底层实现原理
java·开发语言·前端·python·xxl-job
likuolei3 小时前
Spring AI框架完整指南
人工智能·python·spring