简体   繁体   English

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

[英]BeautifulSoup example given in Python documents not working

I am trying out example given in BeautifulSoup documentation and one of the example is not giving intended result 我正在尝试在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)

In the example it says 在它的例子中说

soup.find_all('b')
# [<b>The Dormouse's story</b>]

but when I try the same command I am getting error as below 但是当我尝试相同的命令时,我会收到如下错误

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

but soup object is not 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>

I am not sure why the given example is not working. 我不确定为什么给定的例子不起作用。

You are using BeautifulSoup version three , not version four. 您正在使用BeautifulSoup版本 ,而不是版本四。

In BeautifulSoup 3, the method is called findAll() , not find_all() . 在BeautifulSoup 3中,该方法称为findAll() ,而不是find_all() Because using an attribute that is not recognized is translated to soup.find('unrecognized_attribute') , you asked BeautifulSoup to find you the first <find_all> HTML element , which doesn't exist so None is returned. 因为使用无法识别的属性会被转换为soup.find('unrecognized_attribute') ,所以您要求BeautifulSoup找到第一个<find_all> HTML元素 ,该元素不存在,因此返回None

Use BeautifulSoup 4 instead: 请改用BeautifulSoup 4:

from bs4 import BeautifulSoup

where you almost certainly instead used: 你几乎可以肯定使用的地方:

from BeautifulSoup import BeautifulSoup  # version 3

You'll need to install the beautifulsoup4 project. 您需要安装beautifulsoup4项目。

Demo: 演示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM