【问题标题】:Can you return tags where condition is not met in BeautifulSoup?您可以返回 BeautifulSoup 中不满足条件的标签吗?
【发布时间】:2013-07-20 23:10:54
【问题描述】:

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

我可以用soup.find_all('input', attrs={'type': 'hidden'}) 获得所有hidden 字段,但你不能用attrs!={'type': 'hidden'} 来否定它。

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

【问题讨论】:

    标签: python python-2.7 beautifulsoup


    【解决方案1】:

    您必须使用function match

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

    【讨论】:

    • 谢谢,我不知道这些。它需要tag.name == 'input',而不是tag.name = 'input'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多