如何从lxml.html.html5paser元素标记中删除名称空间值 [英] How to remove namespace value from inside lxml.html.html5paser element tag

查看:90
本文介绍了如何从lxml.html.html5paser元素标记中删除名称空间值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用时是否可能不为标签添加名称空间 来自lxml.html包的html5parser?

Is it possible not to add namespace for the tag when using html5parser from the lxml.html package?

示例:

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'

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

The easiest solution I found is to remove that using regex, but maybe it's possible not to include that text at all?

推荐答案

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

There is a specific namespaceHTMLElements boolean flag that controls this behavior:

from lxml.html import html5parser
from html5lib import HTMLParser

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

这篇关于如何从lxml.html.html5paser元素标记中删除名称空间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆