Remove all html in python?

后端 未结 3 877
情话喂你
情话喂你 2020-12-10 23:12

Is there a way to remove/escape html tags using lxml.html and not beautifulsoup which has some xss issues? I tried using cleaner, but i want to remove all html.

3条回答
  •  悲&欢浪女
    2020-12-10 23:55

    This uses lxml's cleaning functions, but avoids the result being wrapped in an HTML element.

    import lxml
    
    doc = lxml.html.document_fromstring(str) 
    cleaner = lxml.html.clean.Cleaner(allow_tags=[''], remove_unknown_tags=False)
    str = cleaner.clean_html(doc).text_content() 
    

    or as a one liner

    lxml.html.clean.Cleaner(allow_tags=[''], remove_unknown_tags=False).clean_html(lxml.html.document_fromstring(str)).text_content()
    

    It works by providing parsing the html manually into a document object, and giving that to the cleaner class. That way clean_html also returns an object rather than a string. Then the text can be recovered without a wrapper element using text_content() method.

提交回复
热议问题