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

结果示例:

相关推荐
东莞市云毅网络有限公司3 小时前
用 Node 做收录快照的周对比:差集、阈值与告警
python·sqlite·自动化运维·geo·数据监测
山楂树の4 小时前
Python Pandas 数据处理:数据读取、筛选、清洗、排序与分组
开发语言·python·pandas
Logintern095 小时前
langgraph 图编译的聊天机器人
python·langgraph
xurileidian5 小时前
第 22 篇 · 边玩边学Python · 双等号与 is 不是一回事
开发语言·python·spring
钱栈up7 小时前
GitHub热榜实测|Sherlock:一个用户名搜遍全网
python·开源·github
geovindu7 小时前
python: Gradient Boosting
开发语言·后端·python·算法·boosting
Agent世界8 小时前
发布台账字段漂移:1265 条记录里 190 条没有标题
python·自动化运维
用户298698530149 小时前
Python 自动化处理 Word:按规则查找并高亮关键文本
python·api·编程语言
人工智能技术咨询.10 小时前
什么才算高质量数据集
python
DanCheng-studio11 小时前
毕业设计项目 机器视觉指纹识别特征对比算法(源码+论文)
python·毕业设计·毕设