拥有这样的html
somehtml = "<p>Here is my solution: </p><pre><code> some code here </code> </pre> <pre>this is not a code</pre>"
通过python,我想将类“foo”添加到那些包含子
<pre>
的<code>
标记中,因此我的输出将是:somehtml = "<p>Here is my solution: </p><pre class="foo"><code> some code here </code> </pre> <pre>this is not a code</pre>"
我怎样才能做到这一点?
最佳答案
使用lxml,可以这样做:
import lxml.html as lh
import io
somehtml = "<p>Here is my solution: </p><pre><code> some code here </code> </pre> <pre>this is not a code</pre>"
doc=lh.parse(io.BytesIO(somehtml))
root=doc.getroot()
pres=root.xpath('//pre/code/..')
for pre in pres:
pre.attrib['class']='foo'
print(lh.tostring(root))
产量
<html><body><p>Here is my solution: </p><pre class="foo"><code> some code here </code> </pre> <pre>this is not a code</pre></body></html>
关于python - 通过python添加一个类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4767815/