如何用美汤替换HTML元素的值?
How to replace an HTML element's value with beautiful soup?
在 html 文件中搜索一些文本 blob 后,如下所示:
s="the Quick brown fox..."
from bs4 import BeautifulSoup
soup = BeautifulSoup(open('file.html'), 'html.parser')
matches = soup.find_all(lambda x: x.text == s)
for match in matches:
print(match.parent)
<div class="container-box"><div class="title-box">label</div><p span="" style="font-:normal">the Quick brown fox...</p></div>
如何在 html 文件中仅替换匹配条目的 label
字符串?例如,对于上面找到的元素,我想用 tomatoes
?
替换 label
<div class="container-box"><div class="title-box">tomatoes</div><p span="" style="font-:normal">the Quick brown fox...</p></div>
到目前为止我试过这个:
在:
matches = soup.find_all(lambda x: x.text == s)
for match in matches:
target = match.parent.find("div", {"class": "title-box"})
print(target.replace_with("tomatoes"))
输出:
<div class="title-box">label</div>
试试这个:
for match in matches:
#target = match.parent.find('div')
#EDIT
target = match.parent.select_one('div.title-box')
target.replace_with("tomatoes")
soup
输出:
<html><body><div class="container-box">tomatoes<p span="" style="font-:normal">the Quick brown fox...</p></div>
</body></html>
在 html 文件中搜索一些文本 blob 后,如下所示:
s="the Quick brown fox..."
from bs4 import BeautifulSoup
soup = BeautifulSoup(open('file.html'), 'html.parser')
matches = soup.find_all(lambda x: x.text == s)
for match in matches:
print(match.parent)
<div class="container-box"><div class="title-box">label</div><p span="" style="font-:normal">the Quick brown fox...</p></div>
如何在 html 文件中仅替换匹配条目的 label
字符串?例如,对于上面找到的元素,我想用 tomatoes
?
label
<div class="container-box"><div class="title-box">tomatoes</div><p span="" style="font-:normal">the Quick brown fox...</p></div>
到目前为止我试过这个:
在:
matches = soup.find_all(lambda x: x.text == s)
for match in matches:
target = match.parent.find("div", {"class": "title-box"})
print(target.replace_with("tomatoes"))
输出:
<div class="title-box">label</div>
试试这个:
for match in matches:
#target = match.parent.find('div')
#EDIT
target = match.parent.select_one('div.title-box')
target.replace_with("tomatoes")
soup
输出:
<html><body><div class="container-box">tomatoes<p span="" style="font-:normal">the Quick brown fox...</p></div>
</body></html>