python爬虫-数据解析(bs4)
基本知识概念
数据解析原理:
- 标签定位
- 提取标签、标签属性中存储的数据值
bs4数据解析原理:
- 1.实例化一个BeautifulSoup对象,并且将页面原码数据加载到该对象中
- 2.通过调用BeautifulSoup对象中相关的属性或方法进行标签定位和数据提取
环境安装:
pip install bs4
pip install lxml
如何实例化BeautifulSoup对象:
- from bs4 import BeautifulSoup
- 对象的实例化:
1.将本地的html文档中的数据加载到该对象中
fp = open('./test.html','r',encoding='utf-8')
soup = BeautifulSoup(fp,'lxml')
2.将互联网上获取的页面源码加载到该对象中
page_text = response.text
soup = BeautifulSoup(page_text,'lxml')
提供的用于数据解析的方法和属性:
- soup. tagName :返回的是文档中第一次 出现的tagName对应的标签
- soup. find() :
- find( ' tagName ' ) :等同于soup. div
- 属性定位:
- soup. find( 'div' ,class_ , id/attr= ' song' )
- soup. find_ all( 'tagName i ) :返回符合要求的所有标签(列表)
- select:
- select( '某种选择器(id, class, 标签...选择器) ' ),返回的是一个列表 。
- 层级选择器:
- soup. select('.tang > ul > li > a'): >表示的是一个层级
- oup. select(' .tang > ul a'): 空格表示的多个层级
- 获取标签之间的文本数据:
- soup.a. text/string/get_ text( )
- text/get_ text() :可以获取某一个标签中所有的文本内容
- string:只可以获取该标签下面直系的文本内容
- 获取标签中属性值:
- soup.a['href']
bs4实例 —— 爬取三国演义所有章节
需求:爬取三国演义所有章节
https://www.shicimingju.com/book/sanguoyanyi.html
from bs4 import BeautifulSoup
import requests
if __name__ == '__main__':
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
url = 'https://www.shicimingju.com/book/sanguoyanyi.html'
page_text = requests.get(url=url,headers=headers).text
soup = BeautifulSoup(page_text,'lxml')
li_list = soup.select('.book-mulu > ul > li')
fp = open('./三国演义小说.txt','w',encoding='utf-8')
for li in li_list:
title = li.a.string
detail_url = 'https://www.shicimingju.com'+li.a['href']
detail_page_text = requests.get(url=detail_url,headers=headers).text
detail_soup = BeautifulSoup(detail_page_text, 'lxml')
div_tag = detail_soup.find('div',class_='chapter_content')
content = div_tag.text
fp.write('\n' + title + ':' + content +'\n')
print(title,'爬取成功')
发现出现乱码
response.text以文本格式查看的时候有乱码,可能是返回的内容被压缩了,这里修改一下
response.content.decode(“utf-8”) 按utf-8格式输出
from bs4 import BeautifulSoup
import requests
if __name__ == '__main__':
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
url = 'https://www.shicimingju.com/book/sanguoyanyi.html'
page_text = requests.get(url=url,headers=headers).content.decode("utf-8")
soup = BeautifulSoup(page_text,'lxml')
li_list = soup.select('.book-mulu > ul > li')
fp = open('./三国演义小说.txt','w',encoding='utf-8')
for li in li_list:
title = li.a.string
detail_url = 'https://www.shicimingju.com'+li.a['href']
detail_page_text = requests.get(url=detail_url,headers=headers).content.decode("utf-8")
detail_soup = BeautifulSoup(detail_page_text, 'lxml')
div_tag = detail_soup.find('div',class_='chapter_content')
content = div_tag.text
fp.write('\n' + title + ':' + content +'\n')
print(title,'爬取成功')
效果图
练习2—爬取多情剑客无情剑小说所有章节
https://www.gulongwang.com/duo/
from bs4 import BeautifulSoup
import requests
if __name__ == '__main__':
url = 'https://www.gulongwang.com/duo/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
page_text = requests.get(url=url,headers=headers).content.decode('GBK')
soup = BeautifulSoup(page_text,'lxml')
li_list = soup.select('.lb > ul > li')
fp = open('./多情剑客无情剑.txt','w',encoding='utf-8')
for li in li_list:
title = li.a.string
detail_url = 'https://www.gulongwang.com/'+li.a['href']
detail_page_text = requests.get(url=detail_url,headers=headers).content.decode('GBK')
detail_soup = BeautifulSoup(detail_page_text,'lxml')
div_tag = detail_soup.find('div', class_='nr_con')
content = div_tag.text
fp.write('\n'+title+content+'\n')
print(title,'爬取成功')
效果图
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/100110.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...