使用BeautifulSoup的方法如下。这将删除所有不在VALID_TAGS中的标签,但保留已删除标签的内容。
from BeautifulSoup import BeautifulSoupVALID_TAGS = ['div', 'p']soup = BeautifulSoup(value)for tag in soup.findAll('p'): if tag.name not in VALID_TAGS: tag.replaceWith(tag.renderContents())print soup.renderContents()参考



