Python爬取365好书中小说代码实例
作者:Tynam.Yang 时间:2023-05-31 21:25:55
需要转载的小伙伴转载后请注明转载的地址
需要用到的库
from bs4 import BeautifulSoup
import requests
import time
365好书链接:http://www.365haoshu.com/ 爬取《我以月夜寄相思》小说
首页进入到目录:http://www.365haoshu.com/Book/Chapter/List.aspx?NovelId=3026
获取小说的每个章节的名称和章节链接
打开浏览器的开发者工具,查找一个章节:如下图,找到第一章的名称和href(也就是第一章节内容页面的链接),开始写代码
from bs4 import BeautifulSoup
import requests
import time
# 分别导入time、requests、BeautifulSoup库
url = 'http://www.365haoshu.com/Book/Chapter/'
# 链接地址url,这儿url章节链接没全写出来是因为下面获取章节链接时要用到这些url
req = requests.get(url+'List.aspx?NovelId=0326')
# 打开章节页面,
req_bf = BeautifulSoup(req.text,"html.parser")
print(req_bf)
# 将打开的页面以text打印出来
div = req_bf.find_all('div',class_='user-catalog-ul-li')
# 分析页面,所需要的章节名和章节链接是在div标签,属性class为user-catalog-ul-li下
# 找到这个下的内容,并打印
s = []
for d in div:
s.append(d.find('a'))
print(s)
# 获取div下面的a标签下的内容
names=[] # 存储章节名
hrefs=[] # 存储章节链接
for i in s:
names.append(i.find('span').string)
hrefs.append(url + i.get('href'))
# 将所有的章节和章节链接存入的列表中
观察href后的链接和打开章节内容页面的链接是不完全的相同的, 所以要拼接使得浏览器能直接打开章节内容
获取到链接和章节名后打开一个章节获取文本内容;
和获取章节名方法一致,一步一步查找到内容的位置
txt = requests.get(hrefs[0])
div_bf = BeautifulSoup(txt.text,'html.parser')
div = div_bf.find_all('div',class_='container user-reading-online pos-rel')
#print(div)
ps = BeautifulSoup(str(div),"html.parser")
p=ps.find_all('p',class_='p-content')
print(p)
txt=[]
for i in p:
txt.append(i.string+'\n')
print(txt)
获取单一章节完成
接下来整理代码,获取整个小说的内容,代码如下:
# --*-- coding=utf-8 --*--
from bs4 import BeautifulSoup
import requests
import time
class spiderstory(object):
def __init__(self): # 初始化
self.url = 'http://www.365haoshu.com/Book/Chapter/'
self.names = [] # 存放章节名
self.hrefs = [] # 存放章节链接
def get_urlAndName(self):
'''获取章节名和章节链接'''
req = requests.get(url=self.url+'List.aspx?NovelId=0326') # 获取章节目录页面
time.sleep(1) # 等待1秒
div_bf = BeautifulSoup(req.text,"html.parser") # req后面跟text和html都行
div = div_bf.find_all('div', class_='user-catalog-ul-li') # 查找内容,标签为div,属性为class='user-catalog-ul-li'
a_bf = BeautifulSoup(str(div))
a = a_bf.find_all('a') # # 查找内容,标签为a
for i in a:
self.names.append(i.find('span').string) # 获取内容直接string就行
self.hrefs.append(self.url + i.get('href')) # 获取链接
def get_text(self,url):
'''获取章节内容'''
req = requests.get(url=url)
div_bf = BeautifulSoup(req.text,"html.parser")
div = div_bf.find_all('div', class_='container user-reading-online pos-rel') # 查找内容
ps = BeautifulSoup(str(div), "html.parser")
p = ps.find_all('p', class_='p-content')
text = []
for each in p:
text.append(each.string)
print(text)
return text # 将获得的内容返回
def writer(self, name, path, text):
'''写入text文档中'''
with open(path, 'a', encoding='utf-8') as f:
f.write(name + '\n')
f.writelines(text)
f.write('\n\n')
if __name__ == "__main__": # 运行入口
s = spiderstory()
s.get_urlAndName()
le = len(s.names)
for i in range(le): # 利用for循环获得所有的内容
name = s.names[i]
text = str(s.get_text(s.hrefs[i]))
s.writer(name,"我以月夜寄相思.txt",text)
print('下载完毕!!!')
来源:https://www.cnblogs.com/tynam/p/8657031.html
标签:python,爬取,小说
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python中os库的函数使用
2022-05-03 22:16:57
![](https://img.aspxhome.com/file/2023/7/124327_0s.png)
Python3 列表list合并的4种方法
2021-03-28 13:29:43
Go语言使用钉钉机器人推送消息的实现示例
2024-05-09 14:57:37
![](https://img.aspxhome.com/file/2023/4/127064_0s.png)
Python urllib模块urlopen()与urlretrieve()详解
2022-01-17 02:32:02
关于MySQL的sql_mode合理设置详解
2024-01-22 20:44:14
![](https://img.aspxhome.com/file/2023/4/76154_0s.jpg)
Python中给List添加元素的4种方法分享
2023-09-12 04:33:58
ASP中使用Set ors=oConn.Execute()时获取记录数的方法
2011-02-20 10:48:00
Python 正则表达式匹配字符串中的http链接方法
2022-02-11 03:02:13
CentOS 6.5下安装Python 3.5.2(与Python2并存)
2021-06-24 04:33:54
coffeescript使用的方式汇总
2024-04-18 10:02:56
MySQL中distinct语句去查询重复记录及相关的性能讨论
2024-01-16 23:31:27
将Python代码打包为jar软件的简单方法
2023-10-25 17:46:34
python项目对接钉钉SDK的实现
2021-09-18 22:21:29
![](https://img.aspxhome.com/file/2023/9/88929_0s.jpg)
Python实现的企业粉丝抽奖功能示例
2023-10-05 13:57:43
![](https://img.aspxhome.com/file/2023/8/63548_0s.png)
SpringBoot集成内存数据库Derby的实践
2024-01-13 06:02:42
![](https://img.aspxhome.com/file/2023/1/76501_0s.png)
JavaScript 扩展运算符用法实例小结【基于ES6】
2024-04-22 13:06:03
Go语言中DateTime的用法介绍
2024-04-27 15:31:55
python3.6实现学生信息管理系统
2021-02-09 20:54:48
Python列表删除的三种方法代码分享
2022-10-26 19:13:35
Sql Server中常用的6个自定义函数分享
2024-01-17 05:05:40