最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python中lxml中InnerHtml的等价物

Python crifan 5526浏览 0评论

【背景】

折腾:

【记录】Python中尝试用lxml去解析html

中,想要搞懂对于一个ElementTree的节点,如何获得该节点的完整的html,即类似于常见的InnerHtml的效果。

【解决过程】

1.参考:

Equivalent to InnerHTML when using lxml.html to parse HTML

明白了,写成:

from lxml import etree;
h1userElement = htmlElement.find(".//h1[@class='h1user']");
innerHtml = etree.tostring(h1userElement);

即可。

 

【总结】

得到了一个ElementTree节点,然后通过etree.tostring(someElementTree),即可输出InnerHtml了。

转载请注明:在路上 » 【已解决】Python中lxml中InnerHtml的等价物

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 根本不是,这样得到的innerhtml是个非编码的 byte[] ,而且还有乱码问题
    ijun3年前 (2021-02-03)回复
91 queries in 0.164 seconds, using 22.11MB memory