用 lxml.html 替换元素

发布于 2024-08-12 16:48:16 字数 582 浏览 4 评论 0原文

我对 lxml 和 HTML 解析器整体来说还很陌生。 我想知道是否有一种方法可以用另一个元素替换树中的一个元素...

例如我有:

body = """<code> def function(arg): print arg </code> Blah blah blah <code> int main() { return 0; } </code> """

doc = lxml.html.fromstring(body)
codeblocks = doc.cssselect('code')

for block in codeblocks:
  lexer = guess_lexer(block.text_content())
  hilited = highlight(block.text_content(), lexer, HtmlFormatter())
  doc.replace(block, hilited)

我想按照这些思路做一些事情,但这会导致“TypeError”,因为“hilited”不是一个 lxml.etree._Element。

这可行吗?

问候,

I'm fairly new to lxml and HTML Parsers as a whole.
I was wondering if there is a way to replace an element within a tree with another element...

For example I have:

body = """<code> def function(arg): print arg </code> Blah blah blah <code> int main() { return 0; } </code> """

doc = lxml.html.fromstring(body)
codeblocks = doc.cssselect('code')

for block in codeblocks:
  lexer = guess_lexer(block.text_content())
  hilited = highlight(block.text_content(), lexer, HtmlFormatter())
  doc.replace(block, hilited)

I want to do something along those lines, but this results in a "TypeError" because "hilited" isn't an lxml.etree._Element.

Is this feasible?

Regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

×纯※雪 2024-08-19 16:48:16

关于lxml,

doc.replace(block, hilited)中,

block是lxml的Element对象,hilited是字符串,你不能替换它。

有两种方法可以做到这一点

block.text=hilited 

body=body.replace(block.text,hilited)

Regarding lxml,

In doc.replace(block, hilited)

block is the lxml's Element object, hilited is string, you cannot replace that.

There is 2 ways to do that

block.text=hilited 

or

body=body.replace(block.text,hilited)
‘画卷フ 2024-08-19 16:48:16

如果您不熟悉 python HTML 解析器,您可以尝试 BeautifulSoup,一个 html/ xml 解析器,它可以让您轻松修改解析树< /a>.

If you're new to python HTML parsers, you might try out BeautifulSoup, a html/xml parser, which lets you modify the parse tree easily.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文