Find the tag you want to change using find()
search id=test
. Then:
BeautifulSoup Documentation - "Modifying a Tree"
Change .string
If you set the .string tag attribute, the contents of the tags are replaced with the line you give:
markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>' soup = BeautifulSoup(markup) tag = soup.a tag.string = "New link text." tag # <a href="http://example.com/">New link text.</a>
Be careful: if the tag contains other tags, they and all their contents will be destroyed.
source share