Skip to content

Instantly share code, notes, and snippets.

@cansadadeserfeliz
Created May 13, 2015 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cansadadeserfeliz/105ffafab5aca16ebd38 to your computer and use it in GitHub Desktop.
Save cansadadeserfeliz/105ffafab5aca16ebd38 to your computer and use it in GitHub Desktop.
Delinkify
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