检查BeautifulSoup 3中的元素类型 [英] Check element type in BeautifulSoup 3

查看:102
本文介绍了检查BeautifulSoup 3中的元素类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在BS3中检查Tag元素是否为某种类型,例如div?

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':

演示:

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('<div><span></span></div>')
>>> print soup.find('div').name
div

在BeautifulSoup 3和4之间,此属性没有更改.我强烈建议您使用BeautifulSoup 4. BS3上的所有开发都已停止,该版本的最新版本已超过2年.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆