在 BeautifulSoup 3 中检查元素类型 [英] Check element type in BeautifulSoup 3
本文介绍了在 BeautifulSoup 3 中检查元素类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 BS3 中检查 Tag
元素是否属于某种类型,例如 div
?
解决方案
您正在寻找 标签名称:
if element.name == 'div':
演示:
<预><代码>>>>从 bs4 导入 BeautifulSoup>>>汤 = BeautifulSoup('<div><span></span></div>')>>>打印汤.find('div').namediv这个属性在 BeautifulSoup 3 和 4 之间没有变化.我强烈建议你使用 BeautifulSoup 4;BS3 上的所有开发都已停止,该版本的最后一次发布是在 2 年前.
How can I check if a Tag
element is of a certain type, for example a div
, in BS3?
解决方案
You are looking for the tag name:
if element.name == 'div':
Demo:
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('<div><span></span></div>')
>>> print soup.find('div').name
div
This attribute hasn't changed between BeautifulSoup 3 and 4. I strongly recommend you use BeautifulSoup 4; all development on BS3 has stopped, the last release for that version was over 2 years ago.
这篇关于在 BeautifulSoup 3 中检查元素类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文