首页 > 解决方案 > 如何用漂亮的汤替换 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字符串?例如,对于上面找到的元素,我想替换labeltomatoes?

<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>

标签: pythonhtmlbeautifulsouplxml

解决方案


尝试这个:

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>

推荐阅读