BeautifulSoup中find和find_all的使用详解
作者:OCISLU 发布时间:2023-11-08 21:00:22
爬虫利器BeautifulSoup中find和find_all的使用方法
二话不说,先上段HTML例子
<html>
<head>
<title>
index
</title>
</head>
<body>
<div>
<ul>
<li id="flask"class="item-0"><a href="link1.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >first item</a></li>
<li class="item-1"><a href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >second item</a></li>
<li class="item-inactie"><a href="link3.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >third item</a></li>
<li class="item-1"><a href="link4.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fourth item</a></li>
<li class="item-0"><a href="link5.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fifth item</a>
</ul>
</div>
<li> hello world </li>
</body>
</html>
使用BeautifulSoup前需要先构建BeautifulSoup实例
# 构建beautifulsoup实例
soup = BeautifulSoup(html,'lxml')
# 第一个参数是要匹配的内容
# 第二个参数是beautifulsoup要采用的模块,即规则
需要注意的是,导入对的模块需要事先安装,此处导入的LXML事先已经安装。可以导入的模块可通过查询BeautifulSoup的文档查看
接下来是find和find_all的介绍
1. find
只返回第一个匹配到的对象
语法:
find(name, attrs, recursive, text, **wargs)
# recursive 递归的,循环的
参数:
参数名 | 作用 |
---|---|
name | 查找标签 |
text | 查找文本 |
attrs | 基于attrs参数 |
例子:
# find查找一次
li = soup.find('li')
print('find_li:',li)
print('li.text(返回标签的内容):',li.text)
print('li.attrs(返回标签的属性):',li.attrs)
print('li.string(返回标签内容为字符串):',li.string)
运行结果:
find_li: <li class="item-0" id="flask"><a href="link1.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >first item</a></li>
li.text(返回标签的内容): first item
li.attrs(返回标签的属性): {'id': 'flask', 'class': ['item-0']}
li.string(返回标签内容为字符串): first item
find也可以通过‘属性=值'的方法进行匹配
li = soup.find(id = 'flask')
print(li,'\n')
<li class="item-0" id="flask"><a href="link1.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >first item</a></li>
需要注意的是,因为class是python的保留关键字,若要匹配标签内class的属性,需要特殊的方法,有以下两种:
在attrs属性用字典的方式进行参数传递
BeautifulSoup自带的特别关键字class_
# 第一种:在attrs属性用字典进行传递参数
find_class = soup.find(attrs={'class':'item-1'})
print('findclass:',find_class,'\n')
# 第二种:BeautifulSoup中的特别关键字参数class_
beautifulsoup_class_ = soup.find(class_ = 'item-1')
print('BeautifulSoup_class_:',beautifulsoup_class_,'\n')
运行结果
findclass: <li class="item-1"><a href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >second item</a></li>
BeautifulSoup_class_: <li class="item-1"><a href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >second item</a></li>
2. find_all
返回所有匹配到的结果,区别于find(find只返回查找到的第一个结果)
语法:
find_all(name, attrs, recursive, text, limit, **kwargs)
参数名 | 作用 |
---|---|
name | 查找标签 |
text | 查找文本 |
attrs | 基于attrs参数 |
与find一样的语法
上代码
# find_all 查找所有
li_all = soup.find_all('li')
for li_all in li_all:
print('---')
print('匹配到的li:',li_all)
print('li的内容:',li_all.text)
print('li的属性:',li_all.attrs)
运行结果:
---
匹配到的li: <li class="item-0" id="flask"><a href="link1.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >first item</a></li>
li的内容: first item
li的属性: {'id': 'flask', 'class': ['item-0']}
---
匹配到的li: <li class="item-1"><a href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >second item</a></li>
li的内容: second item
li的属性: {'class': ['item-1']}
---
匹配到的li: <li cvlass="item-inactie"><a href="link3.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >third item</a></li>
li的内容: third item
li的属性: {'cvlass': 'item-inactie'}
---
匹配到的li: <li class="item-1"><a href="link4.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fourth item</a></li>
li的内容: fourth item
li的属性: {'class': ['item-1']}
---
匹配到的li: <li class="item-0"><a href="link5.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fifth item</a>
</li>
li的内容: fifth item
附上比较灵活的find_all查询方法:
# 最灵活的使用方式
li_quick = soup.find_all(attrs={'class':'item-1'})
for li_quick in li_quick:
print('最灵活的查找方法:',li_quick)
运行结果:
最灵活的查找方法: <li class="item-1"><a href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >second item</a></li>
最灵活的查找方法: <li class="item-1"><a href="link4.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fourth item</a></li>
完整代码:
# coding=utf8
# @Author= CaiJunxuan
# @QQ=469590490
# @Wechat:15916454524
# beautifulsoup
# 导入beautifulsoup模块
from bs4 import BeautifulSoup
# HTML例子
html = '''
<html>
<head>
<title>
index
</title>
</head>
<body>
<div>
<ul>
<li id="flask"class="item-0"><a href="link1.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >first item</a></li>
<li class="item-1"><a href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >second item</a></li>
<li cvlass="item-inactie"><a href="link3.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >third item</a></li>
<li class="item-1"><a href="link4.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fourth item</a></li>
<li class="item-0"><a href="link5.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fifth item</a>
</ul>
</div>
<li> hello world </li>
</body>
</html>
'''
# 构建beautifulsoup实例
soup = BeautifulSoup(html,'lxml')
# 第一个参数是要匹配的内容
# 第二个参数是beautifulsoup要采用的模块,即规则
# html.parser是python内置的结构匹配方法,但是效率不如lxml所以不常用
# lxml 采用lxml模块
# html5lib,该模块可以将内容转换成html5对象
# 若想要以上功能,就需要具备对应的模块,比如使用lxml就要安装lxml
# 在bs4当中有很多种匹配方法,但常用有两种:
# find查找一次
li = soup.find('li')
print('find_li:',li)
print('li.text(返回标签的内容):',li.text)
print('li.attrs(返回标签的属性):',li.attrs)
print('li.string(返回标签内容为字符串):',li.string)
print(50*'*','\n')
# find可以通过'属性 = 值'的方法进行select
li = soup.find(id = 'flask')
print(li,'\n')
# 因为class是python的保留关键字,所以无法直接查找class这个关键字
# 有两种方法可以进行class属性查询
# 第一种:在attrs属性用字典进行传递参数
find_class = soup.find(attrs={'class':'item-1'})
print('findclass:',find_class,'\n')
# 第二种:BeautifulSoup中的特别关键字参数class_
beautifulsoup_class_ = soup.find(class_ = 'item-1')
print('BeautifulSoup_class_:',beautifulsoup_class_,'\n')
# find_all 查找所有
li_all = soup.find_all('li')
for li_all in li_all:
print('---')
print('匹配到的li:',li_all)
print('li的内容:',li_all.text)
print('li的属性:',li_all.attrs)
# 最灵活的使用方式
li_quick = soup.find_all(attrs={'class':'item-1'})
for li_quick in li_quick:
print('最灵活的查找方法:',li_quick)
来源:https://blog.csdn.net/weixin_42970378/article/details/83108206
猜你喜欢
- 是在CSDN论坛看到的一个问题,平常我也没有注意,或者说没有这样用吧。看代码 <body id="ww
- 压缩包版类似文章有一些问题,缺少了data的初始化,因此结合几个文章完善了一下,希望对Windows上安装者有帮助。MYSQL安装的时候可以
- 前端时间智能信息处理实训,我选择的课题为身份证号码识别,对中华人民共和国公民身份证进行识别,提取并识别其中的身份证号码,将身份证号码识别为字
- 需求是根据当前登录用户来显示某个choice字段不同的选择项。先放现在的实现版本。1、重写PushRuleForm的__init__方法,让
- 记录一次小白的tensorflow学习过程,也为有同样困扰的小白留下点经验。先说我出错和解决的过程。在做风格迁移实验时,使用预加载权重的VG
- 微博模拟登录这是本次爬取的网址:https://weibo.com/一、请求分析找到登录的位置,填写用户名密码进行登录操作看看这次请求响应的
- 一、下载安装官网下载社区版dmg安装文件: https://dev.mysql.com/downloads/mysql/1、执行安装文件,按
- 如果你学过操作系统,那么对于锁应该不陌生。锁的含义是线程锁,可以用来指定某一个逻辑或者是资源同一时刻只能有一个线程访问。这个很好理解,就好像
- 今天想把classification_report的统计结果输出到文件中,我这里分享一下一个简洁的方式:我的pandas版本:pandas
- 前言前段时间因为需要处理一大堆验收单,都是一些简单的复制粘贴替换工作,于是就想到用python进行处理。接下来进入正题~实现需求我是用的开发
- 本文实例为大家分享了H5+css3+js搭建带验证码的登录页面,供大家参考,具体内容如下login.html<!DOCTYPE HTM
- mysql 时间转换函数的用法DAYOFWEEK(date) 返回日期date的星期索引(1=星期天,2=
- 一、前言容器使用沙箱机制,互相隔离,优势在于让各个部署在容器的里的应用互不影响,独立运行,提供更高的安全性。本文主要介绍python应用(d
- 前言其实Python使用QQ邮箱发送Email代码很简单,短短几行代码就可以实现这个功能。使用到的模块有smtplib和email这个两个模
- random() 方法返回随机生成的一个实数,它在[0,1)范围内。random()返回随机生成的一个实数,范围在[0,1)之间语
- 本文实例为大家分享了python实现自动登录后台管理系统的具体代码,供大家参考,具体内容如下首先感谢下网络上的各位大神和博主,通过学习各位大
- 一 MySQL Workbench MySQL Workbench提供DBAs和developers一个集成工具环境: 1)数据库设计和建模
- 前言由于Django是 * 站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,1.
- 朴素贝叶斯估计朴素贝叶斯是基于贝叶斯定理与特征条件独立分布假设的分类方法。首先根据特征条件独立的假设学习输入/输出的联合概率分布,然后基于此
- <?php $link=mysql_connect("localhost",&