Search code examples
pythonhtmlbeautifulsoupescapinghtml-escape-characters

Replace substring with <tag>substring</tag> in BeautifulSoup



I'm trying to modify an existing html file so that specific keywords are printed as strong (no matter where they appear).

My attempt:

from bs4 import BeautifulSoup as soup
txt = """<html><head><style></style></head><body><h2>"This is my keyword</h2><table><tr><td>This could be another instance of the keyword.</td></tr></table></body></html>"""

buzz_words = ["keyword", "apples"]

htmlSoup = soup(txt, features="html.parser")
for word in buzz_words:
    target = htmlSoup.find_all(text=re.compile(r"" + re.escape(word)))
    for v in target:
        v.replace_with(v.replace(word, "".join(["<strong>", word, "</strong>"])))
print(str(htmlSoup))

Result:

This is my &lt ;strong&gt ;keyword&lt ;/strong&gt ;(spaces added by me)

Desired result:

This is my <strong>keyword</strong>

Solution

  • Try the following

    from bs4 import BeautifulSoup as soup
    import re
    import html
    
    txt = """<html><head><style></style></head><body><h2>"This is my keyword</h2><table><tr><td>This could be another instance of the keyword.</td></tr></table></body></html>"""
    
    buzz_words = ["keyword", "apples"]
    
    htmlSoup = soup(txt, features="html.parser")
    for word in buzz_words:
        target = htmlSoup.find_all(text=re.compile(r"" + re.escape(word)))
        for v in target:
            v.replace_with(v.replace(word, "".join(["<strong>", word, "</strong>"])))
    print(html.unescape(str(htmlSoup.prettify())))