This is the perfect place to use a regular expression.
import re
text = 'You can visit <a class="hyperlink" href="https://www.stackoverflow.com">Stack Overflow</a>'
new_text = re.sub(r'href="http(s)?:\/\/(.+?)"', r'href="https://mydomain.com/\2"', text)