python爬取笔趣阁小说
文章目录
前言
通过学习爬取电子书,了解python爬虫的魅力!
一、获取小说目录结构
获取目录连接
可知目录连接:
url = “http://www.paoshuzw.com/13/13959/”
请求代码
import requests
url = "http://www.paoshuzw.com/13/13959/"
res = requests.get(url=url)
with open('圣墟.html','a',encoding='utf-8') as f:
f.write(res.content.decode('utf-8'))
代码注解:
requests.get(url=url) #请求当前连接
res.content.decode(‘utf-8’) #防止中文字符乱码
解析目录
小说目录连接布局
可知
- 章节连接并不是完整的
- 连接并不是自然增长的
- 所以必须获取所有章节连接
点开任意章节
可知
url:www.paoshuzw.com+
/13/13959/5939025.html#章节连接格式
XPath
XPath即为XML路径语言,它是一种用来确定XML(标准通用标记语言的子集)文档中某部分位置的语言。XPath基于XML的树状结构,有不同类型的节点,包括元素节点,属性节点和文本节点,提供在数据结构树中找寻节点的能力。
XPath基本操作:https://blog.csdn.net/Yuyu920716/article/details/113108444
tqdm
Tqdm在阿拉伯语表示进步,在西班牙语中表示我非常爱你。是一个快速,可扩展的Python进度条,可以在Python长循环中添加一个进度提示信息,用户只需要封装任意的迭代器tqdm(iterator)即可完成进度条。
tqdm基本操作:https://blog.csdn.net/feiyang5260/article/details/100050539
解析
import requests
import lxml
from lxml import etree
url = "http://www.paoshuzw.com/13/13959/"
res = requests.get(url=url).content
html = etree.HTML(res)
mulv = [] #存储章节目录
lianjie = [] #存储完整章节连接
with open("圣墟目录.txt",'a',encoding='utf-8') as pic:
L = tqdm(iterable= html.xpath("//dd/a/@href"),total=100,desc="连接进度")
for i in L:
lianjie.append("http://www.paoshuzw.com"+i)
for i in lianjie:
pic.write(i + "\n")
代码注解:
html.xpath("//dd/a/@href") #选中所有章节目录
二、获取小说章节结构
请求代码
import requests
url = "http://www.paoshuzw.com/13/13959/5939025.html"
res = requests.get(url=url)
with open('圣墟第二章.html','a',encoding='utf-8') as f:
f.write(res.content.decode('utf-8'))
代码注解:
requests.get(url=url) #请求当前连接
res.content.decode(‘utf-8’) #防止中文字符乱码
解析章节
小说章节布局
可知
- 章节文本都在
<div id="content"></div>
- 每一个分割线都代表一个段落
- 所以要格式化单章文本
代码
import requests
import lxml
from lxml import etree
url = "http://www.paoshuzw.com/13/13959/5939025.html"
res = requests.get(url=url)
wen = []
with open('圣墟第二章.txt','a',encoding='utf-8') as f:
res = res.content
htmlw = etree.HTML(res)
w = htmlw.xpath("//div[@id='content']/text()")
for i in w:
f.write(' ' + i + '\n')
单章文本:
重构代码
import requests
import lxml
from lxml import etree
from tqdm import tqdm
import time
def getBook(url):
res = requests.get(url=url).content
html = etree.HTML(res)
mulv = [] #章节文本
lianjie = [] #章节连接
wen = [] #单章文本
wens = [] #小说文本
with open("圣墟.txt",'a',encoding='utf-8') as pic:
a = str("\n")
M = tqdm(iterable= html.xpath("//dd/a/text()"),total=100,desc="目录进度")
for i in M:
mulv.append(i + a)
L = tqdm(iterable= html.xpath("//dd/a/@href"),total=100,desc="连接进度")
for i in L:
lianjie.append("http://www.paoshuzw.com"+i)
print("目录:",len(mulv),"连接:",len(lianjie))
for i in lianjie:
res = requests.get(url=i).content
htmlw = etree.HTML(res)
w = htmlw.xpath("//div[@id='content']/text()")
wen = []
for j in w:
wen.append('\n' +' '+j)
wen = ''.join(wen)
wens.append(wen)
time.sleep(1)
print(len(mulv),len(wens))
for i in range(len(mulv)):
pic.write(mulv[i] + '\n' + wens[i] + '\n' + '\n')
print("文件保存成功")
return 1
if __name__ == '__main__':
getBook("http://www.paoshuzw.com/13/13959/")
总结
- 获取请求
- 解析页面
- 分析小说结构
–章节头
–章节文本
–章节格式 - 封装
本文含有隐藏内容,请 开通VIP 后查看