1 回答

TA貢獻1909條經(jīng)驗 獲得超7個贊
我對 lxml 不熟悉。這里還有一個解決方案,僅供大家參考。
from simplified_scrapy import SimplifiedDoc
html = '''
<ac:structured-macro ac:name="center">
<ac:rich-text-body>
<p>
some text
</p>
</ac:rich-text-body>
</ac:structured-macro>
'''
doc = SimplifiedDoc(html)
structuredMacro = doc.select('ac:structured-macro')
structuredMacro.setAttr('ac:name', 'new_center')
# Or
# structuredMacro.setAttrs({'ac:name': 'new_center'})
print(doc.html)
結(jié)果:
<ac:structured-macro ac:name="new_center">
<ac:rich-text-body>
<p>
some text
</p>
</ac:rich-text-body>
</ac:structured-macro>
添加回答
舉報