........<p style=" margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; text-indent:0px;">textHere
<span style=" font-family:'Noto Sans';">ABC</span></p>
<p style=" margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; text-indent:0px;"><span style=" font.......
我有一个像上面这样的HTML。我需要
我试过的是这个,但没有正常工作。
from bs4 import BeautifulSoup
source_code = """.....<span style=" font-family:'Noto Sans';">ABC</span></p>......""
soup = BeautifulSoup(source_code, "lxml")
for re in soup.findAll('font', 'face' = "Noto Sans"):
print (re.replace("A", "X"))
有什么想法?
答案 0 :(得分:1)
您需要找到内置span
的所有font-family: Noto Sans
代码,然后将A
替换为您X
元素中的span
内容实测值:
import re
from bs4 import BeautifulSoup
source_code = """.....<span style=" font-family:'Noto Sans';">ABC</span></p>......"""
soup = BeautifulSoup(source_code, "lxml")
for elm in soup.find_all('span', style=re.compile(r"font-family:'Noto Sans'")):
elm.string = elm.text.replace("A", "X")
print(soup.prettify())
打印:
<span style=" font-family:'Noto Sans';">
XBC
</span>