如何使用 lxml 向 class 添加新值

How to add new value to a class with lxml

不确定它是否可能 tbh.. 我试图做的就是动态编辑文本。

HTML:

 <aside class="banner">
            Place <span class=red>open</span></a>
        </aside>

python:

reds = root.find_class("red")
for element in reds:
    *not sure what goes here*

我已经有了可用于编辑文本遥控器的代码

我知道您问的是 lxml,但是 html 文件的最佳选择是 bs4

bs4/BeautifulSoup 看起来像这样:

for element in soup.find_all("span", { "class": "red"}):
    element.string = NEW_VALUE

with open("out.html", "w") as out_file:
    out_file.write(str(soup))

https://github.com/poleszcz/stack-misc/blob/main/69193852-bs4-edit-content/edit.py