我在使用Beautifulsoup解析带有“class”属性的HTML元素时遇到了麻烦。代码看起来像这样

soup = BeautifulSoup(sdata)
mydivs = soup.findAll('div')
for div in mydivs: 
    if (div["class"] == "stylelistrow"):
        print div

我在脚本完成后的同一行上得到一个错误。

File "./beautifulcoding.py", line 130, in getlanguage
  if (div["class"] == "stylelistrow"):
File "/usr/local/lib/python2.6/dist-packages/BeautifulSoup.py", line 599, in __getitem__
   return self._getAttrMap()[key]
KeyError: 'class'

如何消除这个错误呢?


当前回答

截至BeautifulSoup 4+,

如果你只有一个类名,你可以像这样把类名作为参数传递:

mydivs = soup.find_all('div', 'class_name')

或者如果你有多个类名,只需将类名列表作为参数传递:

mydivs = soup.find_all('div', ['class1', 'class2'])

其他回答

试着先检查div是否有class属性,就像这样:

soup = BeautifulSoup(sdata)
mydivs = soup.findAll('div')
for div in mydivs:
    if "class" in div:
        if (div["class"]=="stylelistrow"):
            print div

从文档中可以看到:

从Beautiful Soup 4.1.2开始,你可以使用关键字参数class_通过CSS类进行搜索:

soup.find_all("a", class_="sister")

在这种情况下是:

soup.find_all("div", class_="stylelistrow")

它还适用于:

soup.find_all("div", class_="stylelistrowone stylelistrowtwo")

这应该可以工作:

soup = BeautifulSoup(sdata)
mydivs = soup.findAll('div')
for div in mydivs: 
    if (div.find(class_ == "stylelistrow"):
        print div

或者我们可以使用lxml,它支持xpath和非常快!

from lxml import html, etree 

attr = html.fromstring(html_text)#passing the raw html
handles = attr.xpath('//div[@class="stylelistrow"]')#xpath exresssion to find that specific class

for each in handles:
    print(etree.tostring(each))#printing the html as string

你可以使用BS3优化你的搜索,只找到那些给定类的div:

mydivs = soup.find_all("div", {"class": "stylelistrow"})