BeautifulSoup - modifying all links in a piece of HTML?

Evan Fosmark picture Evan Fosmark · Jan 20, 2009 · Viewed 13.9k times · Source

I need to be able to modify every single link in an HTML document. I know that I need to use the SoupStrainer but I'm not 100% positive on how to implement it. If someone could direct me to a good resource or provide a code example, it'd be very much appreciated.

Thanks.

Answer

Lusid picture Lusid · Jan 20, 2009

Maybe something like this would work? (I don't have a Python interpreter in front of me, unfortunately)

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup('<p>Blah blah blah <a href="http://google.com">Google</a></p>')
for a in soup.findAll('a'):
  a['href'] = a['href'].replace("google", "mysite")

result = str(soup)