Python文档中给出的BeautifulSoup示例不起作用

2024-04-29 02:03:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试BeautifulGroup文档中给出的示例,其中一个示例没有给出预期的结果

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>;
<p class="story">...</p>
"""

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)

在这个例子中它说

^{pr2}$

但是当我尝试同样的命令时,我得到的错误如下所示

>>> soup.find_all('b')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable

但是汤的对象不是没有

>>> soup

<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    
<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>;
<p class="story">...</p>
</html>

我不知道为什么这个例子不起作用。在


Tags: andthecomidhttptitleexamplehtml
1条回答
网友
1楼 · 发布于 2024-04-29 02:03:15

您使用的是beauthulsoup版本3,而不是版本4。在

在BeautifulGroup 3中,该方法称为findAll(),而不是{}。因为使用未识别的属性被转换为soup.find('unrecognized_attribute'),所以您要求BeautifulGroup为您找到第一个不存在的<find_all>HTML元素,因此返回None。在

改用BeautifulGroup 4:

from bs4 import BeautifulSoup

你几乎可以肯定地用:

^{pr2}$

您需要安装beautifulsoup4项目。在

演示:

>>> 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>;
... <p class="story">...</p>
... """
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(html_doc)
>>> soup.find_all('b')
[<b>The Dormouse's story</b>]
>>> from BeautifulSoup import BeautifulSoup
>>> soup = BeautifulSoup(html_doc)
>>> soup.find_all('b')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable

相关问题 更多 >