【问题标题】:How to replace the innerHTML of all <h1> tags with html5lib?如何用html5lib替换所有<h1>标签的innerHTML?
【发布时间】:2021-07-09 08:29:42
【问题描述】:

如何用html5lib替换所有标签的innerHTML?

输入:

foo
<h1>Moonlight</h1>
bar

期望的输出:

foo
<h1>Sunshine</h1>
bar

我想使用 html5lib,因为它已经是一个依赖项了。

【问题讨论】:

    标签: python html-parsing html5lib


    【解决方案1】:
    from xml.etree import ElementTree
    from html5lib import HTMLParser
    
    parser = HTMLParser(namespaceHTMLElements=False)
    
    tree = parser.parse('''
      foo
      <h1>Moonlight</h1>
      bar''')
    
    for e in tree.findall('.//h1'):
        e.text = 'Sunshine'
    
    print(ElementTree.tostring(etree))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-02
      • 1970-01-01
      • 1970-01-01
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多