如何从 lxml.html.html5paser 元素标签中删除命名空间值

How to remove namespace value from inside lxml.html.html5paser element tag

使用时是否可以不为标签添加命名空间 lxml.html 包中的 html5parser?

示例:

from lxml import html
print(html.parse('http://example.com').getroot().tag)
# You will get 'html'

from lxml.html import html5parser
print(html5parser.parse('http://example.com').getroot().tag)
# You will get '{http://www.w3.org/1999/xhtml}html'

我找到的最简单的解决方案是使用正则表达式删除它,但是 也许根本不包含该文本是可能的?

有一个特定的 namespaceHTMLElements 布尔标志控制此行为:

from lxml.html import html5parser
from html5lib import HTMLParser

root = html5parser.parse('http://example.com', 
                         parser=HTMLParser(namespaceHTMLElements=False))    
print(root.tag)  # prints "html"