dom = BeautifulSoup(untrusted_html) def cleanHtml(dom): for a in dom.findAll('a'): a['rel'] = "nofollow" map(lambda x: x.extract(), dom.findAll(['script', 'object', 'link', 'style'])) return unicode(dom)