python去掉html标签的函数


Python似乎没有像PHP那样方便的函数丢弃掉字符串中所有的HTML标签,下面是一个简单的函数达到该目的。

  1. from HTMLParser import HTMLParser
  2. def strip_tags(html):
  3.     html = html.strip()
  4.     html = html.strip("\n")
  5.     result = []
  6.     parser = HTMLParser()
  7.     parser.handle_data = result.append
  8.     parser.feed(html)
  9.     parser.close()
  10.     return ''.join(result)

感谢您的关注。您现在可以 阅读留言(1)留下通告地址

One Response to “python去掉html标签的函数



Leave a Reply

Note: Any comments are permitted only because the site owner is letting you post, and any comments will be removed for any reason at the absolute discretion of the site owner.

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image