当前位置:网站首页>三国演义小说
三国演义小说
2022-08-02 08:35:00 【赵颂@】
import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
#爬取三国演义小说所有章节标题和章节内容 https://www.shicimingju.com/book/sanguoyanyi.html
if __name__ =='__main__':
headers={
"User-Agent":UserAgent().chrome
}
get_url='https://www.shicimingju.com/book/sanguoyanyi.html'
#发起请求,获取响应
page_text=requests.get(url=get_url,headers=headers).text.encode('ISO-8859-1')
#在首页中解析出章节标题和章节内容
#1. 实例化BeautifulSoup对象,将html数据加载到该对象中
soup=BeautifulSoup(page_text,'lxml')
# print(soup)
#2.解析章节标题和详情页的url
list_data=soup.select('.book-mulu > ul > li')
fp=open('./sanguo.text','w',encoding='utf-8')
for i in list_data:
title=i.a.text
detail_url='https://www.shicimingju.com/'+ i.a['href']
#对详情页的url发送请求,
detail_text=requests.get(url=detail_url,headers=headers).text.encode('ISO-8859-1')
detail_soup=BeautifulSoup(detail_text,'lxml')
#获取章节内容
content=detail_soup.find('div',class_='chapter_content').text
#持久化存储
fp.write(title+":"+content+"\n")
print(title,'下载完成')
边栏推荐
猜你喜欢
随机推荐
(Note)阿克西斯ACASIS DT-3608双盘位硬盘阵列盒RAID设置
Mysql Mac版下载安装教程
ORBSLAM代码阅读
JS中的数组方法
prometheus monitoring mysql_galera cluster
破解wifi密码 暴力破解 保姆式教学
PyQt5(一) PyQt5安装及配置,从文件夹读取图片并显示,模拟生成素描图像
OneinStack多版本PHP共存
oracle的sql改成mysql版本
近期在SLAM建图和定位方面的进展
UVM事务级建模
LeetCode_2358_分组的最大数量
spark:页面单跳转换率统计(案例)
QT web 开发 - 笔记 - 3
主流监控系统工具选型及落地场景参考
UVM信息服务机制
OneNote 教程,如何在 OneNote 中创建更多空间?
【微信小程序2】事件绑定
etcd implements large-scale service governance application combat
pnpm:简介









