开发者

adding a class via python

Having a such html

somehtml = "<p>Here is my solution: </p><pre><code> some code here </code> </pre> <pre>this is not a code</pre>"

Via python I want to add class "foo" to those <pre> tags which contains a child <code> thus my output will be :

somehtml = "<p>Here is my solution: </p><pre class="foo"><code> so开发者_如何学Cme code here </code> </pre> <pre>this is not a code</pre>"

How can I achieve this ?


Using lxml, it could be done like this:

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))

yields

<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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜