等价于使用 lxml.html 解析 HTML 时的 InnerHTML [英] Equivalent to InnerHTML when using lxml.html to parse HTML

查看:36
本文介绍了等价于使用 lxml.html 解析 HTML 时的 InnerHTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用 lxml.html 解析网页的脚本.在我的时间里,我已经做了很多 BeautifulSoup,但由于它的速度,我现在正在尝试使用 lxml.

我想知道在库中什么最明智的方式是做 Javascript 的 InnerHtml 的等价物——即检索或设置标签的完整内容.

<h1>标题</h1><p>一些文字</p>

InnerHtml 因此是:

一个标题

<p>一些文字</p>

我可以使用 hacks(转换为字符串/正则表达式等)来做到这一点,但我假设有一种正确的方法可以使用由于不熟悉而丢失的库来做到这一点.感谢您的帮助.

感谢 pobk 如此快速有效地向我展示了这方面的方法.对于任何尝试相同的人,这就是我的最终结果:

from lxml import html从 cStringIO 导入 StringIOt = html.parse(StringIO("""<正文><h1>标题</h1><p>一些文字</p>未标记的文本<p>未封闭的 p 标签</body>"""))根 = t.getroot()身体 = root.bodyprint (element.text or '') + ''.join([html.tostring(child) for child in body.iterdescendants()])

请注意,lxml.html 解析器将修复未关闭的标签,因此请注意是否存在问题.

解决方案

您可以使用根节点的 getchildren() 或 iterdescendants() 方法获取 ElementTree 节点的子节点:

<预><代码>>>>从 lxml 导入 etree>>>从 cStringIO 导入 StringIO>>>t = etree.parse(StringIO(""")... <h1>标题</h1>... <p>一些文字</p>... </body>"""))>>>根 = t.getroot()>>>对于 root.iterdescendants() 中的孩子,:... 打印 etree.tostring(child)...<h1>标题</h1><p>一些文字</p>

这可以简写如下:

print ''.join([etree.tostring(child) for child in root.iterdescendants()])

I'm working on a script using lxml.html to parse web pages. I have done a fair bit of BeautifulSoup in my time but am now experimenting with lxml due to its speed.

I would like to know what the most sensible way in the library is to do the equivalent of Javascript's InnerHtml - that is, to retrieve or set the complete contents of a tag.

<body>
<h1>A title</h1>
<p>Some text</p>
</body>

InnerHtml is therefore:

<h1>A title</h1>
<p>Some text</p>

I can do it using hacks (converting to string/regexes etc) but I'm assuming that there is a correct way to do this using the library which I am missing due to unfamiliarity. Thanks for any help.

EDIT: Thanks to pobk for showing me the way on this so quickly and effectively. For anyone trying the same, here is what I ended up with:

from lxml import html
from cStringIO import StringIO
t = html.parse(StringIO(
"""<body>
<h1>A title</h1>
<p>Some text</p>
Untagged text
<p>
Unclosed p tag
</body>"""))
root = t.getroot()
body = root.body
print (element.text or '') + ''.join([html.tostring(child) for child in body.iterdescendants()])

Note that the lxml.html parser will fix up the unclosed tag, so beware if this is a problem.

解决方案

You can get the children of an ElementTree node using the getchildren() or iterdescendants() methods of the root node:

>>> from lxml import etree
>>> from cStringIO import StringIO
>>> t = etree.parse(StringIO("""<body>
... <h1>A title</h1>
... <p>Some text</p>
... </body>"""))
>>> root = t.getroot()
>>> for child in root.iterdescendants(),:
...  print etree.tostring(child)
...
<h1>A title</h1>

<p>Some text</p>

This can be shorthanded as follows:

print ''.join([etree.tostring(child) for child in root.iterdescendants()])

这篇关于等价于使用 lxml.html 解析 HTML 时的 InnerHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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