可能重複:
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