Python BeautifulSoup4文档示例不';行不通

Python BeautifulSoup4文档示例不';行不通,python,function,beautifulsoup,Python,Function,Beautifulsoup,我是BeautifulSoup4的新手,学习非常深入。问题在于下一段代码(我在页的文档中找到了它,关于函数定义的文章): 我希望得到这样的结果(见文档): 睡鼠的故事, #从前有……,(B) #…] 但我得到了下一个结果: [<p class="title"><b>The Dormouse's story</b></p>, <p class="story">Once upon a time there were three

我是BeautifulSoup4的新手,学习非常深入。问题在于下一段代码(我在页的文档中找到了它,关于函数定义的文章):


我希望得到这样的结果(见文档):

睡鼠的故事, #

从前有……

,(B) #

] 但我得到了下一个结果:

  [<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 class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,                     
  <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
  <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>; 
  and they lived at the bottom of a well.</p>, <p class="story">...</p>]
睡鼠的故事一次 从前有三个小姐妹,她们的名字叫 , 和 ; 他们住在井底。

] 我检查了Documentation,发现只有方法。has\u attr不推荐使用。没有更多的细节。如何更改初始代码(A)以获得预期结果(B)?有人能帮忙解决这个问题吗?Thnx.

它可以工作。 您必须注意,列表中的第二个结果没有检查内部标记(子标记)中的相同条件。因此包装

已满足条件,并已与所有内容一起放入结果列表中

此结果列表:

[<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 class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
      <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
      <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>; 
      and they lived at the bottom of a well.</p>,
 -------------------------
 <p class="story">...</p>]
睡鼠的故事, -------------------------

一次 从前有三个小姐妹,她们的名字叫 , 和 ; 他们住在井底。

, -------------------------

] 包含三个标记,每个项目都有'class'属性,没有'id'属性。

Doc说:

此函数仅拾取'p'标记。它不拾取'a'标记,因为这些标记定义了“类”和“id”。它不会拾取诸如“html”和“title”之类的标记,因为这些标记不定义“class”

soup.find\u all(有类但没有id)
#睡鼠的故事,
#

从前有, #

]

它不清楚,导致人们期望结果没有任何标记。他们应该更改语句或示例。

“我期望得到这样的结果”-但实际输出是?。(代码中有一个关于
has\u key
的弃用警告,但不确定
has\u attr
:“has\u key已弃用。使用has\u attr(”)相反,请原谅我!是我的错。我在我的请求中修正了它。我更改了代码和文档站点的链接。请再次检查我的问题…为了与Python 3兼容,我重命名了一个方法:Tag.has_key()->Tag.has_attr()-您可以在上面提到的文档页面上查看它…Thnx。但对我来说很重要:如何更改初始代码(A)以获得预期的结果(B)它是拖拉吗?你不需要改变任何事情,不。我只是误解了你的意思。一切都很好。深表歉意。。。
  [<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 class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,                     
  <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
  <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>; 
  and they lived at the bottom of a well.</p>, <p class="story">...</p>]
[<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 class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
      <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
      <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>; 
      and they lived at the bottom of a well.</p>,
 -------------------------
 <p class="story">...</p>]
 soup.find_all(has_class_but_no_id)
# [<p class="title"><b>The Dormouse's story</b></p>,
#  <p class="story">Once upon a time there were...</p>,
#  <p class="story">...</p>]