如何使用BeautifulSoup查找具有未知值属性的元素?

2024-09-27 07:31:12 发布

您现在位置:Python中文网/ 问答频道 /正文

当我们要查找的属性值已知时,就是这样做的:

from bs4 import BeautifulStoneSoup
soup = BeautifulStoneSoup(html, 'html.parser')
found_elems = soup.find_all(attrs={"myattribute" : "myknownvalue"})

在不知道其值的情况下,如何查找具有“myattribute”属性的所有元素


Tags: fromimportparser属性htmlallfindattrs
2条回答

另一种方法是使用CSS选择器:

found_elems = soup.select('[myattribute]')

更多关于CSS选择器here

如果您不知道属性的值,请将其设置为True

from bs4 import BeautifulStoneSoup
soup = BeautifulStoneSoup(html, 'html.parser')
found_elems = soup.find_all(attrs={"myattribute": True})

相关问题 更多 >

    热门问题