擅长:python、mysql、java
<p>您只需使用键设置bs4元素的属性值</p>
<p><strong>代码:</strong></p>
<pre><code>from bs4 import BeautifulSoup as bs
with open("bs-test.svg", "r") as f:
contents = f.read()
soup = bs(contents, "xml")
# grab g tags with the required data-element-id
elem_ls = soup.find_all(attrs={"data-element-id" : "X123456"})
for e in elem_ls:
circle = e.find('circle')
circle['fill'] = 'blue'
print(e)
</code></pre>
<p><strong>结果:</strong></p>
<pre><code><g data-default-color="#FFFFFF" data-element-id="X123456">
<rect class="selection-box" fill="none" height="91" stroke="none" width="140" x="-30" y="-10"/>
<circle cx="40" cy="25" data-colored="true" fill="blue" pointer-events="visible" r="25" stroke="black" stroke-width="3"/>
<text fill="black" font-family="Verdana" font-size="16" text-anchor="middle" x="40" y="55">
<tspan dy="16" x="40">Label Text</tspan>
</text>
</g>
</code></pre>