爬取诗词名句网的三国演义小说

诗词名句网,有很多的诗词和一些课本上古诗的,是一个很好的文学网站,但是我们就来爬取诗词名句网的三国演义小说

第一步我们还是导入要导入的库:
import requests
from bs4 import BeautifulSoup
import os
import time
如果没有安装的可以通过
pip install 库 安装这些库

下面我们开始准备,这次我们要用到的是bs4

我们先来回顾一些bs4的一些知识吧:

bas4解析原理

实例化一个BeautifulSoup的对象,且将待解析的页面源码数据加载到该对象中
调用BeautifulSoup对象中相关方法或者属性进行标签定位和文本数据的提取
环境安装:
pip install lxml#解析器
pip install bs4
BeautifulSoup对象的实例化:
BeautifulSoup(fp,‘lxml’):用来将本地存储的html文档中的数据进行解析
BeautifulSoup(page_text,‘lxml’):用来将互联网上请求到的页面源码数据解析解析
标签定位:
soup.tagName:只可以定位第一次出现的tagName标签
soup.find(‘tagName’,attrName=‘value’);属性定位
soup.findAll:跟find一样用作属性定位,只不过findall返回的是列表
soup.select(“选择器”)
-类选择器
-id选择器
-层级选择
->大于号:表示一个层级
->空格:表示多个层级
取数据
-.text #返回的是该标签下所有的文本内容
-.string #返回的是该标签直系下的文本内容
取属性
-tag[‘attrName’]

上面的这些知识我们在下面的爬取中都要用到:

下面我们正式开始了:

首先我们还是进行一些基本的反爬,就是通过headers来模拟浏览器在发送请求给网站,而不是让浏览器知道我们是机器,让浏览器以为是浏览器在访问

如图1:

main_url='https://www.shicimingju.com/book/sanguoyanyi.html'
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
}
page_text=requests.get(url=main_url,headers=headers)
print(page_text)

我们在上面打印测试拿到浏览器的html时候,我们会发现出现乱码现象,我们就用下面这个解决他

page_text.encoding='UTF-8'

加上编码格式就行了

下面是数据解析:

我们透过bs4来定位html里面的元素:

#数据解析,章节标题,详情页url,章节内容
soup=BeautifulSoup(page,'lxml')
a_list=soup.select('.book-mulu>ul>li>a')

我们这样字就拿到这个如图2的htnl页面内容:

后面我们就通过for循环来拿到所有章节的详情页面的url和标题信息:

for i in a_list:
title=i.string#拿到标题文本,string #返回的是该标签直系下的文本内容
detail_url='http://www.shicimingju.com'+i['href']
#print(title,detail_url)
# #对详情页发起请求解析出章节内容
page_detail=requests.get(url=detail_url,headers=headers)#对章节页面进行再次请求,拿到页面html
page_detail.encoding='UTF-8'#防止乱码,声明编码格式
page_detail=page_detail.text
#print(page_detail)
soup=BeautifulSoup(page_detail,'lxml')
div_tag=soup.find('div',class_="chapter_content")
content=div_tag.text

下面我们就进行爬取的最后一步也就是数据的持久化,数据存储:

#数据持久化,存储小说
path='三国小说'#要存储的文件夹
if not os.path.exists(path):#判断文件夹是否存在
os.mkdir(path)#如果文件夹不存在,就重新创建一个文件夹
with open(path+'./%s'%title+'.txt','w',encoding='utf-8') as file:
file.write(content+'\n')
print("正在下载:"+title)
time.sleep(2)#最基本的反爬虫,通过时间的休眠来让浏览器不会检测到是机器在爬取
file.close()

下面是完整代码:

import requests
from bs4 import BeautifulSoup
import os
import time
path='三国小说'#要存储的文件夹
main_url='https://www.shicimingju.com/book/sanguoyanyi.html'
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
}
page_text=requests.get(url=main_url,headers=headers)
page_text.encoding='UTF-8'
page=page_text.text
#print(page)
#数据解析,章节标题,详情页url,章节内容
soup=BeautifulSoup(page,'lxml')
a_list=soup.select('.book-mulu>ul>li>a')
for i in a_list:
title=i.string
detail_url='http://www.shicimingju.com'+i['href']
#print(title,detail_url)
# #对详情页发起请求解析出章节内容
page_detail=requests.get(url=detail_url,headers=headers)
page_detail.encoding='UTF-8'
page_detail=page_detail.text
#print(page_detail)
soup=BeautifulSoup(page_detail,'lxml')
div_tag=soup.find('div',class_="chapter_content")
content=div_tag.text
# print(content)
#数据持久化,存储小说
if not os.path.exists(path):
os.mkdir(path)
with open(path+'./%s'%title+'.txt','w',encoding='utf-8') as file:
file.write(content+'\n')
print("正在下载:"+title)
time.sleep(2)
file.close()

效果图:

爬取诗词名句网的三国演义小说_xml


爬取诗词名句网的三国演义小说_xml_02