Converting html to text with Python

后端 未结 9 832
一生所求
一生所求 2020-12-12 17:49

I am trying to convert an html block to text using Python.

Input:

9条回答
  •  臣服心动
    2020-12-12 18:53

    There are some nice things here, and i might as well throw in my solution:

    from html.parser import HTMLParser
    def _handle_data(self, data):
        self.text += data + '\n'
    
    HTMLParser.handle_data = _handle_data
    
    def get_html_text(html: str):
        parser = HTMLParser()
        parser.text = ''
        parser.feed(html)
    
        return parser.text.strip()
    

提交回复
热议问题