详解BeautifulSoup获取特定标签下内容的方法


Posted in Python onDecember 07, 2020

以下是个人在学习beautifulSoup过程中的一些总结,目前我在使用爬虫数据时使用的方法的是:先用find_all()找出需要内容所在的标签,如果所需内容一个find_all()不能满足,那就用两个或者多个。接下来遍历find_all的结果,用get_txt()、get(‘href')、得到文本或者链接,然后放入各自的列表中。这样做有一个缺点就是txt的数据是一个单独的列表,链接的数据也是一个单独的列表,一方面不能体现这些数据之间的结构性,另一方面当想要获得更多的内容时,就要创建更多的空列表。

遍历所有标签:

soup.find_all('a')

找出所有页面中含有标签a的html语句,结果以列表形式存储。对找到的标签可以进一步处理,如用for对结果遍历,可以对结果进行purify,得到如链接,字符等结果。

# 创建空列表
links=[] 
txts=[]
tags=soup.find_all('a')
for tag in tags:
  links.append(tag.get('href')
  txts.append(tag.txt)         #或者txts.append(tag.get_txt())

得到html的属性名:

atr=[]
tags=soup.find_all('a')
for tag in tags:
  atr.append(tag.p('class')) # 得到a 标签下,子标签p的class名称

find_all()的相关用法实例:

实例来自BeautifulSoup中文文档

1. 字符串

最简单的过滤器是字符串.在搜索方法中传入一个字符串参数,Beautiful Soup会查找与字符串完整匹配的内容,下面的例子用于查找文档中所有的标签:

soup.find_all('b')
# [<b>The Dormouse's story</b>]

2.正则表达式

如果传入正则表达式作为参数,Beautiful Soup会通过正则表达式的 match() 来匹配内容.下面例子中找出所有以b开头的标签,这表示和标签都应该被找到:

import re
for tag in soup.find_all(re.compile("^b")):
  print(tag.name)
# body
# b

下面代码找出所有名字中包含”t”的标签:

for tag in soup.find_all(re.compile("t")):
  print(tag.name)
# html
# title

3.列表

如果传入列表参数,Beautiful Soup会将与列表中任一元素匹配的内容返回.下面代码找到文档中所有标签和标签:

soup.find_all(["a", "b"])
# [<b>The Dormouse's story</b>,
# <a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

4.方法(自定义函数,传入find_all)

如果没有合适过滤器,那么还可以定义一个方法,方法只接受一个元素参数 [4] ,如果这个方法返回 True 表示当前元素匹配并且被找到,如果不是则反回 False
下面方法校验了当前元素,如果包含 class 属性却不包含 id 属性,那么将返回 True:

def has_class_but_no_id(tag):
  return tag.has_attr('class') and not tag.has_attr('id')```

返回结果中只有

标签没有标签,因为标签还定义了”id”,没有返回和,因为和中没有定义”class”属性.
下面代码找到所有被文字包含的节点内容:

from bs4 import NavigableString
def surrounded_by_strings(tag):
  return (isinstance(tag.next_element, NavigableString)
      and isinstance(tag.previous_element, NavigableString))

for tag in soup.find_all(surrounded_by_strings):
  print tag.name
# p
# a
# a
# a
# p

5.按照CSS搜索

按照CSS类名搜索tag的功能非常实用,但标识CSS类名的关键字 class 在Python中是保留字,使用 class 做参数会导致语法错误.从Beautiful Soup的4.1.1版本开始,可以通过 class_ 参数搜索有指定CSS类名的tag:

soup.find_all("a", class_="sister")
# [<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

或者:

soup.find_all("a", attrs={"class": "sister"})
# [<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

6.按照text参数查找

通过 text 参数可以搜搜文档中的字符串内容.与 name 参数的可选值一样, text 参数接受 字符串 , 正则表达式 , 列表, True . 看例子:

soup.find_all(text="Elsie")
# [u'Elsie']

soup.find_all(text=["Tillie", "Elsie", "Lacie"])
# [u'Elsie', u'Lacie', u'Tillie']

soup.find_all(text=re.compile("Dormouse"))
[u"The Dormouse's story", u"The Dormouse's story"]

def is_the_only_string_within_a_tag(s):
  ""Return True if this string is the only child of its parent tag.""
  return (s == s.parent.string)

soup.find_all(text=is_the_only_string_within_a_tag)
# [u"The Dormouse's story", u"The Dormouse's story", u'Elsie', u'Lacie', u'Tillie', u'...']

虽然 text 参数用于搜索字符串,还可以与其它参数混合使用来过滤tag.Beautiful Soup会找到 .string 方法与 text 参数值相符的tag.下面代码用来搜索内容里面包含“Elsie”的标签:

soup.find_all("a", text="Elsie")
# [<a href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link1">Elsie</a>]

7.只查找当前标签的子节点

调用tag的 find_all() 方法时,Beautiful Soup会检索当前tag的所有子孙节点,如果只想搜索tag的直接子节点,可以使用参数 recursive=False .

一段简单的文档:

<html>
 <head>
 <title>
  The Dormouse's story
 </title>
 </head>
...

是否使用 recursive 参数的搜索结果:

soup.html.find_all("title")
# [<title>The Dormouse's story</title>]

soup.html.find_all("title", recursive=False)
# []

到此这篇关于详解BeautifulSoup获取特定标签下内容的方法的文章就介绍到这了,更多相关BeautifulSoup获取特定标签内容内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
跟老齐学Python之重回函数
Oct 10 Python
Python编程实现二叉树及七种遍历方法详解
Jun 02 Python
Django 生成登陆验证码代码分享
Dec 12 Python
python3+PyQt5实现柱状图
Apr 24 Python
python list转矩阵的实例讲解
Aug 04 Python
pandas 空的dataframe 插入列名的示例
Oct 30 Python
详解Django+Uwsgi+Nginx 实现生产环境部署
Nov 06 Python
python批量创建指定名称的文件夹
Mar 21 Python
python格式化输出保留2位小数的实现方法
Jul 02 Python
python中sort sorted reverse reversed函数的区别说明
May 11 Python
python基础学习之递归函数知识总结
May 26 Python
一篇文章搞懂python混乱的切换操作与优雅的推导式
Aug 23 Python
使用BeautifulSoup4解析XML的方法小结
Dec 07 #Python
BeautifulSoup获取指定class样式的div的实现
Dec 07 #Python
用Python实现童年贪吃蛇小游戏功能的实例代码
Dec 07 #Python
Selenium+BeautifulSoup+json获取Script标签内的json数据
Dec 07 #Python
Python爬虫实战案例之爬取喜马拉雅音频数据详解
Dec 07 #Python
用python对excel查重
Dec 07 #Python
python3 通过 pybind11 使用Eigen加速代码的步骤详解
Dec 07 #Python
You might like
用PHP动态生成虚拟现实VRML网页
2006/10/09 PHP
实例讲解PHP验证邮箱是否合格
2019/01/28 PHP
用javascript实现的仿Flash广告图片轮换效果
2007/04/24 Javascript
JQuery 获取json数据$.getJSON方法的实例代码
2013/08/02 Javascript
Js点击弹出下拉菜单效果实例
2013/08/12 Javascript
js模拟淘宝网的多级选择菜单实现方法
2015/08/18 Javascript
js数组如何添加json数据及js数组与json的区别
2015/10/27 Javascript
JavaScript高级程序设计(第三版)学习笔记6、7章
2016/03/11 Javascript
js实现小窗口拖拽效果
2016/12/03 Javascript
javascript 判断当前浏览器版本并判断ie版本
2017/02/17 Javascript
js实现同一个页面,多个enter事件绑定的示例
2018/10/10 Javascript
Vue中 v-if/v-show/插值表达式导致闪现的原因及解决办法
2018/10/12 Javascript
nodejs简单抓包工具使用详解
2019/08/23 NodeJs
vue与django集成打包的实现方法
2019/11/11 Javascript
[46:04]Liquid vs VP Supermajor决赛 BO 第四场 6.10
2018/07/05 DOTA
Python常用的爬虫技巧总结
2016/03/28 Python
Windows下Python2与Python3两个版本共存的方法详解
2017/02/12 Python
对python多线程与global变量详解
2018/11/09 Python
解决nohup执行python程序log文件写入不及时的问题
2019/01/14 Python
PyQt5显示GIF图片的方法
2019/06/17 Python
python判断自身是否正在运行的方法
2019/08/08 Python
解决django同步数据库的时候app models表没有成功创建的问题
2019/08/09 Python
基于python cut和qcut的用法及区别详解
2019/11/22 Python
Python tkinter布局与按钮间距设置方式
2020/03/04 Python
Python如何实现后端自定义认证并实现多条件登陆
2020/06/22 Python
CSS3+HTML5+JS 实现一个块的收缩与展开动画效果
2020/11/17 HTML / CSS
Html5移动端获奖无缝滚动动画实现示例
2018/06/25 HTML / CSS
HTML5中的强制下载属性download使用实例解析
2016/05/12 HTML / CSS
索引覆盖(Index Covering)查询含义
2012/02/18 面试题
车间主任岗位职责
2014/03/16 职场文书
五好家庭申报材料
2014/12/20 职场文书
财产保全担保书
2015/01/20 职场文书
python某漫画app逆向
2021/03/31 Python
tensorflow+k-means聚类简单实现猫狗图像分类的方法
2021/04/28 Python
解决redis批量删除key值的问题
2022/03/23 Redis
讲解Python实例练习逆序输出字符串
2022/05/06 Python