gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:15:50 25 4
gpt4 key购买 nike

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

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>]

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

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

但是汤对象不是 None

>>> 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>

我不确定为什么给定的示例不起作用。

最佳答案

您使用的是 BeautifulSoup 版本,而不是版本四。

在BeautifulSoup 3中,该方法被称为findAll() ,不是find_all() 。因为使用无法识别的属性会被转换为 soup.find('unrecognized_attribute') ,你让BeautifulSoup第一个找到你<find_all> HTML 元素,该元素不存在,因此 None已返回。

改用 BeautifulSoup 4:

from bs4 import BeautifulSoup

您几乎可以肯定使用的地方:

from BeautifulSoup import BeautifulSoup  # version 3

您需要安装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

关于python - Python 文档中给出的 BeautifulSoup 示例不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28024571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com