Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the text of the inner tag using beautifulsoup python

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 textof 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>
like image 897
null Avatar asked Oct 30 '17 22:10

null


1 Answers

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>
like image 121
PRMoureu Avatar answered Oct 17 '22 06:10

PRMoureu