可能重复:
Beautiful Soup cannot find a CSS class if the object has other classes, tooBeautifulSoup和搜索按类
我使用BeautifulSoup找到在HTML tables
。我目前遇到的问题是使用class
属性中的空格。如果我的HTML读取<html><table class="wikitable sortable">blah</table></html>
,我似乎无法用下面的提取它(我在那里能够找到tables
同为class
都wikipedia
和wikipedia sortable
):
BeautifulSoup(html).findAll(attrs={'class':re.compile("wikitable(sortable)?")})
这会发现,如果表虽然我的HTML只是<html><table class="wikitable">blah</table></html>
。同样,我已经尝试在我的正则表达式中使用"wikitable sortable"
,并且这两者都不匹配。有任何想法吗?
就像更新一样,BeautifulSoup(bs4)的最新版本可以更加优雅地处理这个问题:http://www.crummy.com/software/BeautifulSoup/bs4/doc/#searching-by-css-class – Eli 2013-07-22 20:50:28