BeautifulSoup - modifying all links in a piece of HTML?

后端 未结 3 2190
春和景丽
春和景丽 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 12:58

    I tried this and it worked, it's easier to avoid using regexp for matching each 'href':

    from bs4 import BeautifulSoup as bs
    soup = bs(htmltext)
    for a in soup.findAll('a'):
        a['href'] = "mysite"
    

    Check it out, on bs4 docs.

提交回复
热议问题