使用时是否可以不为标签添加命名空间 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
bool 标志控制此行为:
from lxml.html import html5parser
from html5lib import HTMLParser
root = html5parser.parse('http://example.com',
parser=HTMLParser(namespaceHTMLElements=False))
print(root.tag) # prints "html"
关于python - 如何从 lxml.html.html5paser 元素标记内部删除 namespace 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35012693/