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
猜你喜欢
- 最近发现一个问题,是关于IDEA的一些骚操作的事儿~具体怎么回事,一起来看看。我们都知道使用git分布式版本控制工具,提、拉 代码都会有一个
- remove 删除单个元素,删除首个符合条件的元素,按值删除,返回值为空List_remove = [1, 2, 2, 2, 3, 4]pr
- 之前我们曾经分享过:Python获取某一日期是“星期几”的6种方法!实际上,在我们使用Python处
- 1.安装Python-LDAP(python_ldap-2.4.25-cp27-none-win_amd64.whl)pip install
- 在我发表上一篇《Zen Coding: 一种快速编写HTML/CSS代码的方法》之后,有网友表示不知道怎么在Dreamweaver上使用ze
- Guide to the Section 508 Standards for Electronic and Information Tech
- 摸到她了!青翠的衣衫,奶白的肌肤,捧在手上的感觉真是太好了,心里美滋滋的。《悟透JavaScript》,一本偶然之作,终于成书并出版了。本书
- python的pyaudio可以进行录音,播放,生成wav文件等等,WAVE是录音时用的标准的WINDOWS文件格式,文件的扩展名为WAV,
- 背景说明:10 * time.Second //正常数字相乘没错但是package mainimport "time"f
- 相对于 Ajax,服务端 XMLHTTP 就是在服务端使用 XMLHttpRequest 对象了。虽然说,在服务端使用异步请求是比较不方便的
- Python是一种解释型、面向对象、动态数据类型的高级程序设计语言,本文就举一例Python类继承的实例。实例代码如下:#! /usr/bi
- 考察对于知识的理解,除了实际的代码运用,还有一种方法就是问答类的题型。不同于普通的概念叙述,小编认为即使是面试题也会带有一些数学题目的影响,
- 本次的7个python爬虫小案例涉及到了re正则、xpath、beautiful soup、selenium等知识点,非常适合刚入门pyth
- 标准库的fnmatch库专门用来进行文件名匹配,支持使用通配符进行字符串匹配。1、fnmatch:判断文件名是否符合特定的模式;2、fnma
- 本文实例讲述了Python爬虫爬取杭州24时温度并展示操作。分享给大家供大家参考,具体如下:散点图 爬虫杭州今日24时温度 https://
- 1.3 安装 ASP.net跟基督山一起检查你们的计算机哦CPU Pentium II 450以上,推荐733内存 256M 推荐 512M
- 本教程为大家分享了win10下Python环境安装配置教程,供大家参考,具体内容如下1.在https://www.python.org/do
- 本文实例为大家分享了python批量读取文件名并写入txt中的具体代码,供大家参考,具体内容如下先说下脚本使用的环境吧,在做项目的过程中需要
- 本文实例讲述了Python中的装饰器用法。分享给大家供大家参考。具体分析如下:这里还是先由stackoverflow上面的一个问题引起吧,如
- python中的turtle库是3.6版本中新推出的绘图工具库,那么如何使用呢?下面小编给大家分享一下。首先打开pycharm软件,右键单击