解析HTML/XML并查找原始文档中元素的位置 [英] Parse HTML/XML and find locations of elements in original document

查看:72
本文介绍了解析HTML/XML并查找原始文档中元素的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以获取元素在文档中的原始位置,即.在Python中解析html/xml时的开始和结束字符索引?

Is there a way to get the original location of an element in a document, ie. the start and end character index, when parsing html/xml in Python?

我浏览了lxml文档,找不到任何东西.

I've looked through the lxml documentation and couldn't find anything.

例如

<a>1</a><b>2</b>

...

print tree.find('b').original_position
# result: (9, 16)

推荐答案

Google找到了

Google found this, the gist of which is: it's hard for malformed documents because parsing requires synthesizing valid tokens that don't have any corresponding input. It's possible for valid documents, but most parsing libraries don't support it.

这篇关于解析HTML/XML并查找原始文档中元素的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆