I would like to change the inner text
of a tag in HTML obtained using Beautifulsoup
.
Example:
<a href="index.html" id="websiteName">Foo</a>
turns into:
<a href="index.html" id="websiteName">Bar</a>
I have managed to get the tag by it's id by:
HTMLDocument.find(id='websiteName')
But I'm not beeing able to change the inner text
of the tag:
print HTMLDocument.find(id='websiteName')
a = HTMLDocument.find(id='websiteName')
a = a.replaceWith('<a href="index.html" id="websiteName">Bar</a>')
// I have tried using this as well
a = a.replaceWith('Bar')
print a
Output:
<a href="index.html" id="websiteName">Foo</a>
<a href="index.html" id="websiteName">Foo</a>
Try by changing the string element :
HTMLDocument.find(id='websiteName').string.replace_with('Bar')
from bs4 import BeautifulSoup as soup
html = """
<a href="index.html" id="websiteName">Foo</a>
"""
soup = soup(html, 'lxml')
result = soup.find(id='websiteName')
print(result)
# >>> <a href="index.html" id="websiteName">Foo</a>
result.string.replace_with('Bar')
print(result)
# >>> <a href="index.html" id="websiteName">Bar</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With