我只是浏览了B4文档,得到了一些关于Going back and forth
中html 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>
<;~~这就是困惑
帮助我理解。你知道吗
您仍在查看对标记的引用,当打印该引用时,它包含的所有子项也将打印出来。你知道吗
标记不仅仅是开始
<a ...>
元素,它还包括任何子元素和结束元素。您仍然需要通过.next_element
(也就是u'Tillie'
)访问树中的那些子级。你知道吗在树中导航不会在打开和关闭的文本片段之间移动,而是在树中的元素之间移动。原始的XML/HTML文档以某种顺序定义了这些元素,但这不是您在这里看到的。您看到的是嵌套的标记结构和其他标记内部的文本,一直到根。你知道吗
因此,以下HTML结构:
成为一个沿着以下线条的结构:
(简化为删除大量空白)。你知道吗
如果有对最后一个
a
元素的引用,那么该集合中的前一个元素是文本" and "
,下一个是"Tillie"
。在"Tillie"
之后是文本"; and they lived at the bottom of a well."
。在文本" and "
之前是文本"Lacie"
,等等相关问题 更多 >
编程相关推荐