简体   繁体   English

查找包含特定类的所有html元素

[英]Find all html elements whose contains a specific class

I want BeautifulSoup to find all element in html page whose have a certain class. 我希望BeautifulSoup在html页面中找到具有特定类的所有元素。 But they can also have extra classes. 但是他们也可以有额外的课程。 For example: 例如:

soup.findAll('tr', {'class': 'super_class1'})

This code only finds tr whose have only super_class1 . 此代码仅查找仅具有super_class1 tr But I want it to find all tr whose contains this class such 但是我希望它找到所有包含此类的tr

<tr class='super_class1'>aaa</tr>

and

<tr class='super_class1 super_class2'>bbb</tr>

and

<tr class='super_class1 super_class15  super_class16'>ccc</tr>

This is a bug that has been fixed ( https://bugs.launchpad.net/beautifulsoup/+bug/410304 ); 这是已修复的错误( https://bugs.launchpad.net/beautifulsoup/+bug/410304 ); the problem is basically that the soup doesn't recognizes spaces in class name. 问题基本上是汤不能识别类名中的空格。

But if you have to use a version without the fix, the above link also provides a solution: 但是,如果您必须使用没有此修复程序的版本,则上面的链接还提供了一种解决方案:

soup.findAll(True, {'class': re.compile(r'\bsuper_class1\b')})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM