python之BeautifulSoup

BeautifulSoup怎么用

BeautifulSoup库目前已经进阶到第4版(Beautiful Soup 4),由于它不是Python标准库,而是第三方库,需要单独安装它,不过,我们的学习系统已经安装好了。

解析数据:
在这里插入图片描述

根据之前所学的requests.get(),我们可以先获取到一个Response对象,并确认自己获取成功:
# 调用requests库
import requests
# 获取网页源代码,得到的res是response对象
res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html') 
# 检查请求是否正确响应
print(res.status_code)
# 把res的内容以字符串的形式返回
html = res.text 
# 打印html
print(html)

上面的代码是第0关学过的内容,好,接下来就轮到BeautifulSoup登场解析数据了,请特别留意第2行和第6行新增的代码。

import requests
# 引入BS库,下面的bs4就是beautifulsoup4
from bs4 import BeautifulSoup
res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html')
# 把网页解析为BeautifulSoup对象
soup = BeautifulSoup(res.text,'html.parser') 

使用BeautifulSoup去解析数据:

from bs4 import BeautifulSoup
soup = BeautifulSoup(字符串,'html.parser')

使用BeautifulSoup来提取数据:
这一步,又可以分为两部分知识:find()与find_all(),以及Tag对象(标签对象)。
在这里插入图片描述
1)find()与find_all()是BeautifulSoup对象的两个方法,它们可以匹配html的标签和属性,把BeautifulSoup对象里符合要求的数据都提取出来。
2) find()只提取首个满足要求的数据。find()方法将代码从上往下找,找到符合条件的第一个数据,不管后面还有没有满足条件的其他数据,停止寻找,立即返回。
3)而find_all()顾名思义(find all:查找全部),提取出的是所有满足要求的数据。代码从上往下找,一直到代码的最后,把所有符合条件的数据揣好,一起打包返回。
在这里插入图片描述
在BeautifulSoup中,不止find()和find_all(),还有select()也可以达到相同目的。
在这里插入图片描述

例子
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')
# 使用find()方法提取首个<div>元素,并放到变量item里。
item = soup.find('div') 
# 打印item的数据类型
print(type(item))
# 打印item  
print(item)       		
结果:
<class 'bs4.element.Tag'>
<div>大家好,我是一个块</div>
说明这是一个Tag类标签对象。
例子
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')
# 用find_all()把所有符合要求的数据提取出来,并放在变量items里
items = soup.find_all('div') 
# 打印items的数据类型
print(type(items)) 
# 打印items
print(items)       
结果:
<class 'bs4.element.ResultSet'>
[<div>大家好,我是一个块</div>, 
<div>我也是一个块</div>, 
<div>我还是一个块</div>]
打印items的类型,显示的是<class 'bs4.element.ResultSet'>,是一个ResultSet类的对象。其实是Tag对象以列表结构储存了起来,可以把它当做列表来处理。
Tag类对象的常用属性和方法

在这里插入图片描述
我们用Tag.text提出Tag对象中的文字,用Tag[‘href’]提取出URL。

在这里插入图片描述
上一关,我们的操作对象从URL链接到了Response对象。而这一关,我们的操作对象是这样的:Response对象——字符串——BS对象。到这里,又产生了两条分岔:一条是BS对象——Tag对象;另一条是BS对象——列表——Tag对象。

在这里插入图片描述

例子
import requests
from bs4 import BeautifulSoup
res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html')
html = res.text
soup = BeautifulSoup( html,'html.parser')
items = soup.find_all(class_='books')
for item in items:
    kind = item.find('h2')
    title = item.find(class_='title')
    brief = item.find(class_='info')
    print(kind.text,'\n',title.text,'\n',title['href'],'\n',brief.text)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值