python - 如何使用Beautiful4过滤多类

标签 python beautifulsoup

<分区>

from bs4 import BeautifulSoup

html = """
    <div class="aa bb"></div>
    <div class="aa ccc"></div>
    <div class="aa"></div>
"""


def find(aclass):
    print(aclass)
    return aclass != "bb"

soup = BeautifulSoup(html, 'lxml')

div = soup.find_all('div', attrs={'class': find})

print(div)

我只想要 class='aa',而不是 'aa bb' 或任何其他。 请帮我! 谢谢!!

最佳答案

这是一个答案 BeautifulSoup webscraping find_all( ): finding exact match

这将只为您提供带有“aa”类的标签。

div = soup.find_all(lambda tag: tag.name == 'div' and tag.get('class') == ['aa'])

关于python - 如何使用Beautiful4过滤多类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34873056/

相关文章:

python - 延时图像的亮度/直方图归一化

python - 从我保存在本地文件系统上的配置单元查询输出中删除空行

python - 在Python中从字典中提取值

python - Python 中的 FileNotFoundError : [Errno 2] No such file or directory using os. 取消链接

html - 如何只从 HTML 中获取新闻部分?

python - 使用 BeautifulSoup/Python 从 html 文件中提取文本

python - Odoo 10 每行报告表

python - 在 beautifulsoup4 中具有子元素的标签内提取文本节点

python - 使用 beautiful soup python 循环抓取 URL

python - 如何使用 BeautifulSoup 基于 Python 中的数据自动化属性从 div 类中抓取内容?