Search code examples
pythonbeautifulsoup

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 its id by:

HTMLDocument.find(id='websiteName')

But I'm not 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>

Solution

  • 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>