用lxml.html替换元素 [英] Replacing elements with lxml.html
本文介绍了用lxml.html替换元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对lxml和HTML解析器作为一个整体还很陌生. 我想知道是否有一种方法可以将树中的一个元素替换为另一个元素...
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...
例如我有:
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",因为被人为"不是lxml.etree._Element.
I want to do something along those lines, but this results in a "TypeError" because "hilited" isn't an lxml.etree._Element.
这可行吗?
此致
推荐答案
关于lxml,
在doc.replace(block, hilited)
block 是lxml的Element对象,被是字符串,您不能替换它.
block is the lxml's Element object, hilited is string, you cannot replace that.
有2种方法可以做到这一点
There is 2 ways to do that
block.text=hilited
或
body=body.replace(block.text,hilited)
这篇关于用lxml.html替换元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文