Created
May 13, 2015 10:03
-
-
Save cansadadeserfeliz/105ffafab5aca16ebd38 to your computer and use it in GitHub Desktop.
Delinkify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bs4 import BeautifulSoup | |
import bleach | |
def _delinkify(self, text): | |
""" | |
Converts <a href="http:my.url">Text</a> to | |
<a href="http:my.url">Text: http:my.url</a> | |
""" | |
soup = BeautifulSoup(text) | |
for link in soup.find_all('a'): | |
link.insert_after( | |
soup.new_string(u': {0}'.format(link.get('href'))) | |
) | |
return soup | |
# Remove all the html tags | |
tpl_txt = bleach.clean( | |
_delinkify(tpl_html), | |
tags=[], attributes=[], styles=[], strip=True, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment