BeautifulSoup - modifying all links in a piece of HTML?

后端 未结 3 2228
春和景丽
春和景丽 2020-12-01 12:03

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.

3条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 13:03

    from BeautifulSoup import BeautifulSoup
    soup = BeautifulSoup('

    Blah blah blah Google

    ') for a in soup.findAll('a'): a['href'] = a['href'].replace("google", "mysite") print str(soup)

    This is Lusid's solution, but since he didn't have a Python interpreter in front of him, he wasn't able to test it and it had a few errors. I just wanted to post the working condition. Thank's Lusid!

提交回复
热议问题