使用Beautiful Soup的find_all函数查找文本,并将其转换为字符串,然后使用replace函数替换文本中的关键字。以下是示例代码:
from bs4 import BeautifulSoup
html = """
This is a test string for Beautiful Soup
"""
soup = BeautifulSoup(html, 'html.parser')
text = soup.find_all(text=True)
keywords1 = ['Beautiful Soup', 'test string']
keywords2 = ['example', 'another keyword']
for i in text:
if i.parent.name == 'p':
for word in keywords1:
if word in i:
i.replace_with(str(i).replace(word, '' + word + ''))
for word in keywords2:
if word in i:
i.replace_with(str(i).replace(word, '' + word + ''))
print(soup)
这将输出经过修改的HTML代码,其中包含了关键字1和关键字2的强调标记。注意,我们只替换了p标记中的文本,而不是修改整个HTML代码。