.next\u元素和.previous\u元素soup4的概念冲突

2024-10-08 18:24:17 发布

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

我只是浏览了B4文档,得到了一些关于Going back and forthhtml family tree的概念。你知道吗

last_a_tag = soup.find("a", id="link3")
last_a_tag
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
last_a_tag.next_element
# u'Tillie'  
last_a_tag.previous_element
# u' and\n' ## upto this is Good to understand!
last_a_tag.previous_element.next_element
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

冲突在这里浮现在我的脑海里。根据.Previous_element概念last_a_tag.previous_element.next_elementt应该给出<a class="sister" href="http://example.com/tillie" id="link3">但是为什么要给出上面所示的完整的一个呢?你知道吗

编辑

last_a_tag.previous_element
# u' and\n'  <~~Perfect
last_a_tag.previous_element.next_element
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

为什么不等到下面呢?你知道吗

#<a class="sister" href="http://example.com/tillie" id="link3">

下半部分怎么走? Tillie</a><;~~这就是困惑

帮助我理解。你知道吗


Tags: comidhttpexampletagelementsisterclass
1条回答
网友
1楼 · 发布于 2024-10-08 18:24:17

您仍在查看对标记的引用,当打印该引用时,它包含的所有子项也将打印出来。你知道吗

标记不仅仅是开始<a ...>元素,它还包括任何子元素和结束元素。您仍然需要通过.next_element(也就是u'Tillie')访问树中的那些子级。你知道吗

在树中导航不会在打开和关闭的文本片段之间移动,而是在树中的元素之间移动。原始的XML/HTML文档以某种顺序定义了这些元素,但这不是您在这里看到的。您看到的是嵌套的标记结构和其他标记内部的文本,一直到根。你知道吗

因此,以下HTML结构:

<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>;
and they lived at the bottom of a well.</p>

成为一个沿着以下线条的结构:

p
\
  a
  \
    "Elsie"
  ", "
  a
  \
    "Lacie"
  " and "
  a
  \
    "Tillie"
  "; and they lived at the bottom of a well."

(简化为删除大量空白)。你知道吗

如果有对最后一个a元素的引用,那么该集合中的前一个元素是文本" and ",下一个是"Tillie"。在"Tillie"之后是文本"; and they lived at the bottom of a well."。在文本" and "之前是文本"Lacie",等等

相关问题 更多 >

    热门问题