使用Regex删除html标记

2024-03-29 13:38:01 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here

我试图摆脱的HTML标记,在一定程度上它的工作,但不是所有的标记被删除。但下面提到的标签并没有消失

print('NOT DEALT WITH:')
for body in not_dealt_with_list:
#p = re.compile(r'<.*?[\\t\\n\\r\\s]*?.*?>')
    print(remove_tags(body))
    #print(p.sub('', body))
    #body = re.sub()

def remove_tags(content):
parser = lxml.html.HTMLParser(remove_comments=True, 
remove_blank_text=True)
document = lxml.html.document_fromstring(content, parser)
return document.text_content()

Tags: text标记retrueparserhtmltagsbody
1条回答
网友
1楼 · 发布于 2024-03-29 13:38:01

看起来您要删除的内容嵌入到了一个html注释中(因为它看起来不像html)。Html注释以开头,这就是您必须搜索的内容。你知道吗

尝试使用这个正则表达式搜索注释中的所有内容,然后在多行中替换它

<! (.|\n)*? >

告诉我结果如何!你知道吗

相关问题 更多 >