BeautifulSoup: 如何用 span 标签替换内容
BeautifulSoup: how to replace content with in span tag
........<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。我需要
- 找到'Noto Sans' font-family中的所有内容(它们总是在span标签内)
- 替换它们(A 为 X,B 为 Y 等......)而不改变其余代码
我试过的是这个,但是不能正常工作。
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"))
有什么想法吗?
您需要找到所有包含 font-family: Noto Sans
的 span
标签,然后将每个 span
元素中的 A
替换为 X
发现:
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>
........<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。我需要
- 找到'Noto Sans' font-family中的所有内容(它们总是在span标签内)
- 替换它们(A 为 X,B 为 Y 等......)而不改变其余代码
我试过的是这个,但是不能正常工作。
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"))
有什么想法吗?
您需要找到所有包含 font-family: Noto Sans
的 span
标签,然后将每个 span
元素中的 A
替换为 X
发现:
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>