使用 lxml 查找文本和子元素的顺序
假设我有以下 HTML:
<div>
text1
<div>
t1
</div>
text2
<div>
t2
</div>
text3
</div>
我知道如何使用 lxml.html 获取封闭 div 的文本和子元素。但是有没有一种方法可以以迭代方式访问文本和子元素,同时保留顺序?换句话说,我想知道 div 的“自由文本”相对于图像出现在哪里。我希望能够知道“text1”出现在第一个内部 div 之前,并且 text2 出现在两个内部 div 之间,等等。
Let's say I have the following HTML:
<div>
text1
<div>
t1
</div>
text2
<div>
t2
</div>
text3
</div>
I know of how to get the text and subelements of the enclosing div using lxml.html. But is there a way to access both text and sub elements in an iterative manner, that preserves order? In other words, I want to know where the "free text" of the div appears relative to the images. I would like to be able to know that "text1" appears before the first inner-div, and that text2 appears between the two inner-divs, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
elementtree
接口(lxml
也提供)支持这一点——例如,使用 Python 2.7 中的内置元素树:根据您的 lxml/elementtree 版本,您可以需要拼写迭代器方法
.getiterator()
而不是.iter()
。如果您需要一个按顺序生成标签和文本的生成器,例如:
这基本上删除了
None
并生成第一项为'tag'
的二元组>、'text'
或'tail'
,帮助您区分。我想这不是您理想的格式,但将其塑造成更符合您喜好的格式应该不难;-)。The
elementtree
interface, whichlxml
also offers, supports that -- e.g. with the built-in element tree in Python 2.7:Depending on your version of lxml/elementtree, you may need to spell the iterator method
.getiterator()
instead of.iter()
.If you need a single generator that will yields tags and texts in order, for example:
This basically removes the
None
s and yields two-tuples with a first item of'tag'
,'text'
, or'tail'
, to help you distinguish. I imagine this is not your ideal format, but it should not be hard to mold it into something more to your liking;-).