如果对象也有其他类,Beautiful Soup 找不到 CSS 类 [英] Beautiful Soup cannot find a CSS class if the object has other classes, too
本文介绍了如果对象也有其他类,Beautiful Soup 找不到 CSS 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果一个页面有
和
,那么 soup.findAll(True, 'class1')
会找到它们.如果它有 <p class="class1 class2">
,但是,它不会被找到.如何找到具有某个类的所有对象,而不管它们是否也有其他类?
解决方案
以防万一有人遇到这个问题.BeautifulSoup 现在支持这个:
Python 2.7.5(默认,2013 年 5 月 15 日,22:43:36)[MSC v.1500 32 位(英特尔)]输入版权"、信用"或许可"以获取更多信息.在 [1]:导入 bs4在[2]中:soup = bs4.BeautifulSoup('<div class="foo bar"></div>')在 [3]: 汤(attrs={'class': 'bar'})出[3]:[<div class="foo bar"></div>]
此外,您不必再输入 findAll.
if a page has <div class="class1">
and <p class="class1">
, then soup.findAll(True, 'class1')
will find them both.
If it has <p class="class1 class2">
, though, it will not be found. How do I find all objects with a certain class, regardless of whether they have other classes, too?
解决方案
Just in case anybody comes across this question. BeautifulSoup now supports this:
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
In [1]: import bs4
In [2]: soup = bs4.BeautifulSoup('<div class="foo bar"></div>')
In [3]: soup(attrs={'class': 'bar'})
Out[3]: [<div class="foo bar"></div>]
Also, you don't have to type findAll anymore.
这篇关于如果对象也有其他类,Beautiful Soup 找不到 CSS 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文