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

结果示例:

相关推荐
Ronin-Lotus28 分钟前
深度学习篇---Opencv中的机器学习和深度学习
python·深度学习·opencv·机器学习
信阳农夫1 小时前
Django解析跨域问题
后端·python·django
m0_371356151 小时前
【测试语言基础篇】Python基础之List列表
开发语言·python·list
大0马浓2 小时前
训练大模型LLM选择哪种开发语言最好
人工智能·python·训练
风筝超冷2 小时前
AttributeError: module ‘backend_interagg‘ has no attribute ‘FigureCanvas‘
python
onejason2 小时前
如何利用爬虫获取腾讯新闻详情数据:实战指南
前端·python
yicode2 小时前
Python基础:列表与元组详解
后端·python
梦丶晓羽3 小时前
自然语言处理:无监督朴素贝叶斯模型
人工智能·python·自然语言处理·tf-idf·贝叶斯定理·词袋模型·无监督朴素贝叶斯模型
Y雨何时停T3 小时前
使用 Python 批量提取 PDF 书签:一款实用工具的实现
python·pdf