我對lxml和HTML Parsers整體來說還是比較新的。 我想知道如果有一種方法可以替換另一個元素樹中的元素...用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。
這可行嗎?
問候,
我一直在摸索,我發現BeautifulSoup必須通過replaceWith這個確切的功能,有沒有類似的東西-this-在lxml中? (如果不是這樣還好,我仍然感興趣) 再次感謝! – tsoporan