首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >BeautifulSoup文档中给出的不工作的示例

BeautifulSoup文档中给出的不工作的示例
EN

Stack Overflow用户
提问于 2015-01-19 12:22:23
回答 1查看 264关注 0票数 1

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

代码语言:javascript
运行
复制
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)

在这个例子中,它说

代码语言:javascript
运行
复制
soup.find_all('b')
# [<b>The Dormouse's story</b>]

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

代码语言:javascript
运行
复制
>>> soup.find_all('b')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable

但汤对象不是零

代码语言:javascript
运行
复制
>>> 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>

我不知道为什么这个例子行不通。

EN

Stack Overflow用户

回答已采纳

发布于 2015-01-19 12:27:27

您使用的是BeautifulSoup版本3,而不是version 4。

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

使用BeautifulSoup 4代替:

代码语言:javascript
运行
复制
from bs4 import BeautifulSoup

您几乎可以肯定地使用:

代码语言:javascript
运行
复制
from BeautifulSoup import BeautifulSoup  # version 3

您需要安装beautifulsoup4项目。

演示:

代码语言:javascript
运行
复制
>>> 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
票数 3
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28024571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档