python - BeautifulSoup 中不满足条件的标签可以返回吗?

标签 python python-2.7 beautifulsoup

我正在尝试获取input页面中的标签,但我不想返回任何带有 type 属性 hidden 的标签.

我可以获得所有hidden包含 soup.find_all('input', attrs={'type': 'hidden'}) 的字段但你不能仅仅用 attrs!={'type': 'hidden'} 来否定这一点.

是否有一种简单的单行方法来获取所有与给定属性上的条件匹配的标签?

最佳答案

您必须使用 function match :

def input_not_type_hidden(tag):
    return tag.name == 'input' and tag.get('type') != 'hidden'

soup.find_all(input_not_type_hidden)

关于python - BeautifulSoup 中不满足条件的标签可以返回吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17783813/

相关文章:

python - 如何检查字符串中的 unicode 或转义序列?

python - 在 python 中发送 GET 请求的有效负载

python - 使用 beautifulsoup 查找所有强标签会返回一个空数组

python - 从 BeautifulSoup 对象获取 URL

Python:列表的 2 个索引位置括号

python - 步长不等的迭代

python - 将字符串中的每个字符更改为字母表中的下一个字符

python - 美汤刮痧 : Why won't the get_text method return the text of this element?

python - PCA:获取前 20 个最重要的维度

Python 2.7,AT命令响应,奇怪的HEX