Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将子字符串替换为<;标签>;子串</标签>;在美丽的乌苏 我正试图修改一个现有的html文件,以便将特定的关键字打印为strong(无论它们出现在何处)。_Python_Html_Beautifulsoup_Escaping_Html Escape Characters - Fatal编程技术网

Python 将子字符串替换为<;标签>;子串</标签>;在美丽的乌苏 我正试图修改一个现有的html文件,以便将特定的关键字打印为strong(无论它们出现在何处)。

Python 将子字符串替换为<;标签>;子串</标签>;在美丽的乌苏 我正试图修改一个现有的html文件,以便将特定的关键字打印为strong(无论它们出现在何处)。,python,html,beautifulsoup,escaping,html-escape-characters,Python,Html,Beautifulsoup,Escaping,Html Escape Characters,我的尝试: 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 instan

我的尝试:

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))
期望结果:

This is my &lt ;strong&gt ;keyword&lt ;/strong&gt ;(spaces added by me)
This is my <strong>keyword</strong>
这是我的关键字
尝试以下操作

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())))
从bs4导入BeautifulSoup作为汤
进口稀土
导入html
txt=“”这是我的关键字这可能是该关键字的另一个实例。”“”
buzz_words=[“关键字”,“苹果”]
htmlSoup=soup(txt,features=“html.parser”)
对于buzz_单词中的单词:
target=htmlSoup.find_all(text=re.compile(r“”+re.escape(word)))
对于目标中的v:
v、 替换为(v.replace(单词“”。join([“”,单词“”]))
打印(html.unescape(str(htmlSoup.prettify()))

这很有效,只是对我的代码做了一点小小的调整。非常感谢您的快速帮助!