Strip HTML from strings in Python

前端 未结 26 2336
难免孤独
难免孤独 2020-11-22 02:50
from mechanize import Browser
br = Browser()
br.open(\'http://somewebpage\')
html = br.response().readlines()
for line in html:
  print line

When p

26条回答
  •  时光说笑
    2020-11-22 03:19

    If you need to preserve HTML entities (i.e. &), I added "handle_entityref" method to Eloff's answer.

    from HTMLParser import HTMLParser
    
    class MLStripper(HTMLParser):
        def __init__(self):
            self.reset()
            self.fed = []
        def handle_data(self, d):
            self.fed.append(d)
        def handle_entityref(self, name):
            self.fed.append('&%s;' % name)
        def get_data(self):
            return ''.join(self.fed)
    
    def html_to_text(html):
        s = MLStripper()
        s.feed(html)
        return s.get_data()
    

提交回复
热议问题