【问题标题】:Remove html tags using Regex使用正则表达式删除 html 标签
【发布时间】:2019-11-25 05:19:43
【问题描述】:

我试图摆脱 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()

【问题讨论】:

    标签: python html regex beautifulsoup


    【解决方案1】:

    看起来您要删除的内容已嵌入到 html 注释中(因为它看起来不像 html)。 Html cmets 开头,这就是您必须搜索的内容。

    试试这个正则表达式来搜索评论中的所有内容,然后在多行中替换它

    <!--(.|\n)*?-->
    

    告诉我结果如何!

    【讨论】:

      猜你喜欢
      • 2010-10-21
      • 2011-04-16
      • 2011-05-02
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      相关资源
      最近更新 更多