BeautifulSoup是什么
在爬虫中,须要使用能读懂html的工具,才能提取到想要的数据。这就是解析数据。
【提取数据】是指把咱们须要的数据从众多数据中挑选出来。
解析与提取数据在爬虫中,既是一个重点,也是难点
BeautifulSoup怎么用
安装beautifulsoup ==>
pip install BeautifulSoup4
在括号中,要输入两个参数,第0个参数是要被解析的文本,注意了,它必须必须必须是字符串。
括号中的第1个参数用来标识解析器,咱们要用的是一个Python内置库:
html.parser
import requests #调用requests库
res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html')
#获取网页源代码,获得的res是response对象
print(res.status_code) #检查请求是否正确响应
html = res.text #把res的内容以字符串的形式返回
print(html)#打印html
复制代码
import requests
from bs4 import BeautifulSoup
#引入BS库
res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html')
html = res.text
soup = BeautifulSoup(html,'html.parser') #把网页解析为BeautifulSoup对象
print(type(soup)) # <class 'bs4.BeautifulSoup'>
复制代码
打印
soup
出来的源代码和咱们以前使用response.text
打印出来的源代码是彻底同样的虽然
response.text
和soup
打印出的内容表面上看长得如出一辙,它们属于不一样的类:
<class 'str'>
与<class 'bs4.BeautifulSoup'>
提取数据
咱们仍然使用
BeautifulSoup
来提取数据。这一步,又能够分为两部分知识:
find()
与find_all()
,以及Tag对象
。
find()
与find_all()
是BeautifulSoup
对象的两个方法,它们能够匹配html的标签和属性,把BeautifulSoup对象里符合要求的数据都提取出来
区别在于,
find()
只提取首个知足要求的数据,而find_all()
提取出的是全部知足要求的数据
localprod.pandateacher.com/python-manu…
HTML代码中,有三个
<div>
元素,用find()
能够提取出首个元素,而find_all()
能够提取出所有
import requests
from bs4 import BeautifulSoup
url = 'https://localprod.pandateacher.com/python-manuscript/crawler-html/spder-men0.0.html'
res = requests.get (url)
print(res.status_code)
soup = BeautifulSoup(res.text,'html.parser')
item = soup.find('div') #使用find()方法提取首个<div>元素,并放到变量item里。
print(type(item)) #打印item的数据类型
print(item) #打印item
复制代码
import requests
from bs4 import BeautifulSoup
url = 'https://localprod.pandateacher.com/python-manuscript/crawler-html/spder-men0.0.html'
res = requests.get (url)
print(res.status_code)
soup = BeautifulSoup(res.text,'html.parser')
items = soup.find_all('div') #用find_all()把全部符合要求的数据提取出来,并放在变量items里
print(type(items)) #打印items的数据类型
print(items) #打印items
复制代码
举例中括号里的
class_
,这里有一个下划线,是为了和python语法中的类class
区分,避免程序冲突小练习: 爬取网页中的三本书的书名、连接、和书籍介绍
localprod.pandateacher.com/python-manu…
另外一个知识点——
Tag
对象。
咱们通常会选择用
type()
函数查看一下数据类型,Python是一门面向对象编程的语言,只有知道是什么对象,才能调用相关的对象属性和方法。
用
find()
提取出来的数据类型和刚才同样,仍是Tag
对象
咱们能够用
Tag.text
提出Tag
对象中的文字,用Tag['href']
提取出URL
import requests #调用requests库
from bs4 import BeautifulSoup
# 获取数据
res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html')
# res.status_code 状态码
# res.content 二进制
# res.text html代码
# res.encoding 编码
# 解析数据
# soup 是beautifulsoup对象
soup = BeautifulSoup(res.text,'html.parser')
# soup.find(标签名,属性=属性值)
# soup.find_all(标签名, 属性=属性值)
# 提取数据 list 里面是tag对象
item = soup.find_all('div',class_='books')
for i in item:
# i.find().find().find() # tag对象, 能够一级一级找下去
# i.find_all()
# i 是tag对象, 也可使用find和find_all, 获得结果仍是tag对象
# i.find().find().find().find()
print(i.find('a',class_='title').text) # 获取标签内容
print(i.find('a',class_='title')['href']) # 获取标签属性(href)
print(i.find('p',class_='info').text) # 获取标签内容
复制代码
层层检索的过程有点像是在超市买你想要的零食
对象的变化过程
最开始
requests
获取数据,到BeautifulSoup
解析数据,再用BeautifulSoup
提取数据不断经历的是咱们操做对象的类型转换
完整版
完整版
再复习一遍代码...
import requests #调用requests库
from bs4 import BeautifulSoup
# 获取数据
res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html')
# res.status_code 状态码
# res.content 二进制
# res.text html代码
# res.encoding 编码
# 解析数据
# soup 是beautifulsoup对象
soup = BeautifulSoup(res.text,'html.parser')
# soup.find(标签名,属性=属性值)
# soup.find_all(标签名, 属性=属性值)
# 提取数据 list 里面是tag对象
item = soup.find_all('div',class_='books')
for i in item:
# i.find().find().find() # tag对象, 能够一级一级找下去
# i.find_all()
# i 是tag对象, 也可使用find和find_all, 获得结果仍是tag对象
# i.find().find().find().find()
print(i.find('a',class_='title').text) # 获取标签内容
print(i.find('a',class_='title')['href']) # 获取标签属性(href)
print(i.find('p',class_='info').text) # 获取标签内容
复制代码
总结
beautifulsoup 解析器
解析器 | 使用方法 | 优点 | 劣势 |
---|---|---|---|
Python标准库 | BeautifulSoup(text, "html.parser") | Python的内置标准库执行速度适中文档容错能力强 | Python 2.7.3 or 3.2.2前的版本中文档容错能力差 |
lxml HTML 解析器 | BeautifulSoup(text, "lxml") | 速度快文档容错能力强 | 须要安装C语言库 |
lxml XML 解析器 | BeautifulSoup(text, "xml") | 速度快惟一支持XML的解析器 | 须要安装C语言库 |
html5lib | BeautifulSoup(text, "html5lib") | 生成HTML5格式的文档 | 速度慢不依赖外部扩展 |
做业1:爬取文章, 并保存到本地(每一个文章, 一个html文件)
wordpress-edu-3autumn.localprod.forc.work
做业2: 爬取分类下的图书名和对应价格, 保存到books.txt
books.toscrape.com
最终效果...
猫哥教你写爬虫 000--开篇.md
猫哥教你写爬虫 001--print()函数和变量.md
猫哥教你写爬虫 002--做业-打印皮卡丘.md
猫哥教你写爬虫 003--数据类型转换.md
猫哥教你写爬虫 004--数据类型转换-小练习.md
猫哥教你写爬虫 005--数据类型转换-小做业.md
猫哥教你写爬虫 006--条件判断和条件嵌套.md
猫哥教你写爬虫 007--条件判断和条件嵌套-小做业.md
猫哥教你写爬虫 008--input()函数.md
猫哥教你写爬虫 009--input()函数-人工智能小爱同窗.md
猫哥教你写爬虫 010--列表,字典,循环.md
猫哥教你写爬虫 011--列表,字典,循环-小做业.md
猫哥教你写爬虫 012--布尔值和四种语句.md
猫哥教你写爬虫 013--布尔值和四种语句-小做业.md
猫哥教你写爬虫 014--pk小游戏.md
猫哥教你写爬虫 015--pk小游戏(全新改版).md
猫哥教你写爬虫 016--函数.md
猫哥教你写爬虫 017--函数-小做业.md
猫哥教你写爬虫 018--debug.md
猫哥教你写爬虫 019--debug-做业.md
猫哥教你写爬虫 020--类与对象(上).md
猫哥教你写爬虫 021--类与对象(上)-做业.md
猫哥教你写爬虫 022--类与对象(下).md
猫哥教你写爬虫 023--类与对象(下)-做业.md
猫哥教你写爬虫 024--编码&&解码.md
猫哥教你写爬虫 025--编码&&解码-小做业.md
猫哥教你写爬虫 026--模块.md
猫哥教你写爬虫 027--模块介绍.md
猫哥教你写爬虫 028--模块介绍-小做业-广告牌.md
猫哥教你写爬虫 029--爬虫初探-requests.md
猫哥教你写爬虫 030--爬虫初探-requests-做业.md
猫哥教你写爬虫 031--爬虫基础-html.md
猫哥教你写爬虫 032--爬虫初体验-BeautifulSoup.md
猫哥教你写爬虫 033--爬虫初体验-BeautifulSoup-做业.md
猫哥教你写爬虫 034--爬虫-BeautifulSoup实践.md
猫哥教你写爬虫 035--爬虫-BeautifulSoup实践-做业-电影top250.md
猫哥教你写爬虫 036--爬虫-BeautifulSoup实践-做业-电影top250-做业解析.md
猫哥教你写爬虫 037--爬虫-宝宝要听歌.md
猫哥教你写爬虫 038--带参数请求.md
猫哥教你写爬虫 039--存储数据.md
猫哥教你写爬虫 040--存储数据-做业.md
猫哥教你写爬虫 041--模拟登陆-cookie.md
猫哥教你写爬虫 042--session的用法.md
猫哥教你写爬虫 043--模拟浏览器.md
猫哥教你写爬虫 044--模拟浏览器-做业.md
猫哥教你写爬虫 045--协程.md
猫哥教你写爬虫 046--协程-实践-吃什么不会胖.md
猫哥教你写爬虫 047--scrapy框架.md
猫哥教你写爬虫 048--爬虫和反爬虫.md
猫哥教你写爬虫 049--完结撒花.mdhtml