Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Libxml清理器添加了不需要的<;p>;标记到HTML片段_Python_Parsing_Libxml2 - Fatal编程技术网

Python Libxml清理器添加了不需要的<;p>;标记到HTML片段

Python Libxml清理器添加了不需要的<;p>;标记到HTML片段,python,parsing,libxml2,Python,Parsing,Libxml2,我正在尝试使用libxml的HTML清理程序清理用户输入,以防止XSS注入。当我输入如下字符串时: Normal text <b>Bold text</b> 另一方面,上面的代码有一行:allow_tags=None我试图删除所有HTML标记。libxml是否有白名单功能,其中我只允许某些标记?所有的文本片段/节点必须包含在某种元素中libxml将尝试尽可能地解决此问题 def sanitize_html(html): cleaned_html = clean

我正在尝试使用libxml的HTML清理程序清理用户输入,以防止XSS注入。当我输入如下字符串时:

Normal text <b>Bold text</b>


另一方面,上面的代码有一行:
allow_tags=None
我试图删除所有HTML标记。libxml是否有白名单功能,其中我只允许某些标记?

所有的
文本
片段/节点必须包含在某种元素中
libxml
将尝试尽可能地解决此问题

def sanitize_html(html):
    cleaned_html = cleaner.clean_html(html)
    return re.sub(r'</p>$', '', re.sub(r'^<p>', '', cleaned_html))

保留在
标记中会导致什么问题?@Karl:我正在通过Markdown传递清理后的输出,Markdown将忽略块级标记中的任何内容进行格式化。这也打乱了我的其他CSS规则。谢谢你让我知道所有东西都需要封闭,我没有意识到这一点。我的解决方案实际上是在清理之前将文本放入另一个标记中。
from lxml.html import clean

cleaner = clean.Cleaner(
    scripts = True,
    javascript = True,
    allow_tags = None,
)

def sanitize_html(html):
    return cleaner.clean_html(html)
def sanitize_html(html):
    cleaned_html = cleaner.clean_html(html)
    return re.sub(r'</p>$', '', re.sub(r'^<p>', '', cleaned_html))
return cleaned_html[3:-4]     # Single slice operation
return cleaned_html[3:][:-4]