Python Beautiful Soup .content属性


问题内容

BeautifulSoup的.content有什么作用?我正在遍历crummy.com的教程,但我不太了解.content的作用。我看过这些论坛,但没有看到任何答案。看下面的代码。

from BeautifulSoup import BeautifulSoup
import re



doc = ['<html><head><title>Page title</title></head>',
       '<body><p id="firstpara" align="center">This is paragraph <b>one</b>.',
        '<p id="secondpara" align="blah">This is paragraph <b>two</b>.',
        '</html>']

soup = BeautifulSoup(''.join(doc))
print soup.contents[0].contents[0].contents[0].contents[0].name

我希望代码的最后一行打印出“ body”,而不是…

  File "pe_ratio.py", line 29, in <module>
    print soup.contents[0].contents[0].contents[0].contents[0].name
  File "C:\Python27\lib\BeautifulSoup.py", line 473, in __getattr__
    raise AttributeError, "'%s' object has no attribute '%s'" % (self.__class__.__name__, attr)
AttributeError: 'NavigableString' object has no attribute 'name'

.content是否仅与html,head和title有关?如果是,那为什么呢?

我在这里先向您的帮助表示感谢。


问题答案:

它只是为您提供标记 的内容。让我用一个例子演示:

html_doc = """
<html><head><title>The Dormouse's story</title></head>

<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)
head = soup.head

print head.contents

上面的代码给了我一个清单,[<title>The Dormouse's story</title>]因为这就是 里面
head标签。因此,致电[0]会给您列表中的第一项。

出现错误的原因是因为soup.contents[0].contents[0].contents[0].contents[0]返回的内容没有其他标签(因此没有属性)。它Page Title从您的代码返回,因为第一个contents[0]给您HTML标记,第二个给您head标记。第三个指向title标签,第四个为您提供实际内容。因此,当您调用name它时,它没有标签可提供。

如果要打印正文,可以执行以下操作:

soup = BeautifulSoup(''.join(doc))
print soup.body

如果只想body使用contents,请使用以下命令:

soup = BeautifulSoup(''.join(doc))
print soup.contents[0].contents[1].name

您不会将其[0]用作索引,因为它body是之后的第二个元素head