当前位置:网站首页>爬虫-爬取某小说网站
爬虫-爬取某小说网站
2022-08-10 08:20:00 【爱学习的Amelia】
- 需求:要爬取某个小说所有章节的网址,再通过爬虫将每一章的内容爬取下来,再本地创建一个文件夹,并且把小说分别保存到这个文件夹中。每一章保存为一个文件。
- 思路:首先是再网址里面获取每一章节的链接,再解析每一个章节的链接中获得文章的标题以及内容,之后再存储。
- 代码如下所示:
import re
import os
import requests
# 获取每一章的链接,拼接成网址
def get_toc(html):
""" 获取每一章的连接存储到一个列表中并返回 :param html:目录页源代码 :return:每章节链接 """
toc_url_list = []
start_url = 'https://www.kanunu8.com/book3/6879/'
toc_block = re.findall('正文(.*?)</tbody>', html, re.S)[0]
toc_url = re.findall('href="(.*?)"', toc_block, re.S)
for url in toc_url:
toc_url_list.append(start_url + url)
return toc_url_list
def get_article(html):
""" 获取每一章的正文并返回章节名和正文 :param html:正文源代码 :return:章节名,正文 """
chapter_name = re.search('size="4">(.*?)<', html, re.S).group(1)
text_block = re.search('<p>(.*?)</p>', html, re.S).group(1)
text_block = text_block.replace('<br />', '')
text_block = re.sub('\r\n\r\n', '\r\n', text_block)
return chapter_name, text_block
# 保存的代码
def save(chapter, article):
""" 将每一章保存到本地。 :param chapter:章节名,第X章 :param article:正文内容 :return:None """
os.makedirs('文件名称', exist_ok=True)
# 如果没有“文件名称”文件夹,就创建一个,图国有,则什么都不做
with open(os.path.join('文件名称', chapter + '.txt'), 'w', encoding='utf-8') as f:
f.write(article)
# 解析网页
html_html = requests.get('目标网址').content.decode('gbk')
strs = get_toc(html_html)
for i in strs:
# 解析内部的每一个小网页
html_each = requests.get(i).content.decode('gbk')
save(get_article(html_each)[0], get_article(html_each)[1])
- 以上代码仅供参考,我爬取的网站为:https://www.kanunu8.com/book3/6879/
边栏推荐
- 速卖通卖家如何抓住产品搜索权重
- m.bjhjwy.com全面教学设备 类型包括: 教学仪器, 教学设备 ,
- 关于数据中心的设计方案,数据中心网络规划设计
- 全连接神经网络结构图,神经网络示意图怎么画
- 问下cdc mysql to doris.不显示具体行数,怎么办?
- Rust learning: 6.3_ Tuples of composite types
- Go-Excelize API源码阅读(十一)—— GetActiveSheetIndex()
- DGIOT supports industrial equipment rental and remote control
- 预测股票涨跌看什么指标,如何预测明天股票走势
- If the data of the oracle business table is added, deleted, or modified, will the index of the table write redo and undo?
猜你喜欢

Rust learning: 6.1_Slices of composite types

Rust学习:6.3_复合类型之元组

2022-08-01 网工进阶(二十三) VLAN高级技术-VLAN聚合、MUX VLAN

机器人控制器编程实践指导书旧版-实践二 传感器(模拟量)

PLSQL学习第一天

机器人控制器编程实践指导书旧版-实践一 LED灯(数字量)

Based on STC8G2K64S4 single-chip microcomputer to display analog photosensitive analog value through OLED screen

Solve the problem that the win10win7win8 system cannot find the specified module and cannot register the desert plug-in

ABAP Data Types 和XSD Type 映射关系以及XSD Type属性

AFNetworking概述和4.0的实践
随机推荐
机器人控制器编程实践指导书旧版-实践二 传感器(模拟量)
A File Online Query Display and Download Function Realized by Delphi
初使jest 单元测试
[In-depth study of 4G/5G/6G topic-56]: L3 signaling control-5-radio bearer management
90.(cesium之家)cesium高度监听事件
人工神经网络模型的特点,人工神经网络模型定义
Add spark related dependencies and packaging plugins (sixth bullet)
phpstudy starts automatically
Ask next CDC mysql to Doris. Don't show the specific number of lines, how to do?
同步锁synchronized追本溯源
并查集模板
组合数模板
WooCommerce installation and rest api usage
Unity—UGUI control
.NET-7.WPF learning experience summary
明明加了唯一索引,为什么还是产生重复数据?
差分、前缀和模板
Rust learning: 6.1_Slices of composite types
nrm 使用详解
不同的命令行风格