e-s*_*tis 38

Python标准库中没有这样的东西.这是因为Python是一种通用语言,而PHP最初是一种面向Web的语言.

不过,您有3个解决方案:

  • 你很匆忙:只做自己的.re.sub(r'<[^>]*?>', '', value)是一个快速而肮脏的解决方案.
  • 使用第三方库(推荐因为更多防弹):美丽的汤是一个非常好的,没有什么可以安装,只需复制lib目录和导入.完整的tuto与美丽的汤.
  • 使用框架.大多数Web Python开发人员从不编写代码,他们使用像django这样的框架来自动为您提供这些东西.django全tuto.


Joh*_*ooy 15

使用BeautifulSoup

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(htmltext)
''.join([e for e in soup.recursiveChildGenerator() if isinstance(e,unicode)])
Run Code Online (Sandbox Code Playgroud)


小智 7

from bleach import clean
print clean("<strong>My Text</strong>", tags=[], strip=True, strip_comments=True)
Run Code Online (Sandbox Code Playgroud)