基于键编辑XML值

2024-04-26 01:37:52 发布

您现在位置:Python中文网/ 问答频道 /正文

使用beautifulsoup4,如何基于名称编辑值?假设我知道name=“foo”(常量和唯一值),那么如何读取值“bar”(运行时未知),并将其更改为新字符串“baz”?你知道吗

我可以用汤。全部找到(text=“foo”),但我不知道如何方便地读取值,或更改字符串“bar”--->;“baz”

XML摘录:

<djmacros>
  <macro name="foo" value="bar">
    <description>foobar</description>
  </macro>
</djmacros>

Tags: 字符串textnamegt名称编辑foobar
1条回答
网友
1楼 · 发布于 2024-04-26 01:37:52

我没有试过,但我怀疑这样的方法可能有用:

foo_element = soup.find('macro', attrs={"name" : "foo"}) # returns the foo element
foo_element['value'] = "baz" # changes the value attribute

相关问题 更多 >

    热门问题