用python分章节保存小说

 用python下载我喜欢的一本小说《散落星河的记忆》

718a09e9661243c7a3e3d78f82f39733.png

下载小说首先引入一些所需要的第三方库,requests库用来访问网页源代码,os是与操作系统进行交互功能,用于寻找文件路径,pool是进行多线程操作,加快程序的运行速度,re是正则表达式,用于提取自己想要的内容

import requests
import os
from multiprocessing.dummy import Pool
import re

建立一个获取网页源代码的函数

def get_source(url):
    html = requests.get(url).content.decode()
    return html

 这是小说网页的源代码,里面有小说每个章节的网址链接

3636f6de1691459ea6957c47b746585a.png

下来用正则表达式从上面获取到的网页源代码中获取每个章节的网址链接,并保存在一个列表中

def get_url(html):
    html_lists=[]
    html_list = re.findall('href="(https://www.zhenhunxiaoshuo.com/121.*?)">', html, re.S) 
    for html1 in html_list:
        html_lists.append(html1)
    return html_lists

再用正则表达式从每个章节网址的源代码中获取小说章节目录和正文

def get_article(html1):
    chapter_name = re.search('<h1 class="article-title">(第.*?)</h1>', html1, re.S).group(1)
    article_=re.search('<article class="article-content"><p>(.*?)</article>',html1,re.S).group(1)
    article_=article_.replace('<p>','').replace('</p>','')
    return chapter_name,article_

建立并保存小说文件的函数(分章节保存)

def save(chapter,article):
    os.makedirs('散落星河的记忆',exist_ok=True)
    with open(os.path.join('散落星河的记忆',chapter+'.txt'),'a',encoding='UTF-8') as f:
        f.write(article)

利用上面的函数构建一个获取小说章节源代码并保存章节名称和正文的函数

def toc_article(url):
    html1=get_source(url)
    chapter,article=get_article(html1)
    save(chapter,article)

利用多线程去爬取这本小说,提高爬取效率

import requests
import os
from multiprocessing.dummy import Pool
import re
 
start_url='https://www.zhenhunxiaoshuo.com/sanluoxinghedejiyi/'
def get_source(url):
    html = requests.get(url).content.decode()
    return html
 
def get_url(html):
    html_lists=[]
    html_list = re.findall('href="(https://www.zhenhunxiaoshuo.com/121.*?)">', html, re.S)
    for html1 in html_list:
        html_lists.append(html1)
    return html_lists
 
def get_article(html1):
    chapter_name = re.search('<h1 class="article-title">(第.*?)</h1>', html1, re.S).group(1)
    article_=re.search('<article class="article-content"><p>(.*?)</article>',html1,re.S).group(1)
    article_=article_.replace('<p>','').replace('</p>','')
    return chapter_name,article_
 
def save(chapter,article):
    os.makedirs('散落星河的记忆',exist_ok=True)
    with open(os.path.join('散落星河的记忆',chapter+'.txt'),'a',encoding='UTF-8') as f:
        f.write(article)
 
def toc_article(url):
    html1=get_source(url)
    chapter,article=get_article(html1)
    save(chapter,article)
 
if __name__ =='__main__':
    novel_html=get_source(start_url)
    list=get_url(novel_html)
    pool=Pool(45)
    pool.map(toc_article,list)

运行代码后,会自动建立一个文件夹,将小说每个章节以txt的文件类型分别保存在文件夹内。

02f7edc4b53043b9b325f371f89d4775.png

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

倦客.lpc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值