I am aware that it is possible to linkify URLs that have not yet become html links, and Bleach will automatically add rel="nofollow". (Source: http://bleach.readthedocs.io/en/latest/linkify.html)
But how do I add the nofollow attribute to URLs that are already html links (i.e. they are already <a> tags)?
It's an old question, but since it still shows up in search results, I think it's worth answering anyway.
Bleach's
linkify()does process both the pre-existing<a>links and link-like text. So all you need to addrel="nofollow"to all the links in the html fragment is to calllinkify()Or, if only the pre-existing links need to be processed, a custom filter may be used to discard all new links: