Python .next_元素和.previous_元素的概念冲突4
我刚刚浏览了B4文档,了解了Python .next_元素和.previous_元素的概念冲突4,python,python-2.7,beautifulsoup,Python,Python 2.7,Beautifulsoup,我刚刚浏览了B4文档,了解了html家族树中的来回的一些概念 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' ##
html家族树中的来回的一些概念
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>
为什么不等到下面呢
#<a class="sister" href="http://example.com/tillie" id="link3">
#
您仍在查看标记的引用,打印该引用时,它包含的所有子项也将打印出来
标记不仅仅是开始元素,它还包括所有子元素和结束元素。例如,您仍然需要通过.next\u 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>
(简化为删除大量空白)
如果您引用了最后一个元素
,则该集合中的前一个元素是文本“和”
,下一个元素是“Tillie”
。“蒂莉”
后面是文本“他们住在井底。”
。在文本“和”
之前是文本“Lacie”
,等等。…因此您希望标记下一个元素。上一个元素!=标签!=标记。上一个元素。下一个元素
@凯特莉亚,亚历克斯,看来你没有理解我的困惑!你能重读一下描述吗。我不是反对任何事情,而是试图理解其中的逻辑。如果你清楚,请帮助我进入你的水平!。。。是一个元素,若这是你们的困惑,那个么它和来回无关bit@PythonLikeYOU当前位置确实,我不明白你的困惑。我完全不明白你当时的要求。你似乎在说你不希望。下一个元素。上一个元素
什么都不做。你为什么这么想?好吧!看来我明白了!现在只需告诉我如何最后一个\u标记。下一个\u元素
只给出\u'Tillie'
@PythonLikeYOU:文本块也是元素。您有一个,而不是一个常规的python unicode对象。他们也有.next.*
和.previous.*
参考文献。我无法连接它在两个方向上的工作方式,只能理解一种方式。定义对我来说只有.next\u element
方法是可以理解的,而不是。previous\u element
。为什么只有一个方向可以理解next_元素
表示“第一个孩子”,除非没有孩子,否则表示“下一个兄弟姐妹”,除非没有兄弟姐妹,否则表示“下一个兄弟姐妹”,然后向上递归父代,直到再次出现下一个兄弟姐妹previous_元素
表示内部的上一个同级(或者说,递归地说,它是最后一个子元素),直到没有同级为止,在这种情况下,它转到父级。如果从根元素开始,使用。下一个同级元素可以访问树中的每个元素,然后使用。上一个元素
,按相反顺序返回。@PythonLikeYOU:该顺序恰好与您在文档中查找元素的方式相匹配。打开标记->开始一个元素,将其添加到父元素中,这是接下来每件事情的新父元素。Closing tag->done with this element,close it,使其父元素成为我们添加下一个解析元素的新父元素。
<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."