2011-01-22 58 views
2

添加类有一个这样的HTML通过Python

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

通过Python我想补充类“富”,其包含一个子<code>那些<pre>标签因此我的输出将是:

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

我该如何做到这一点?

+0

你创建自己的HTML? – 2011-01-22 12:36:13

+0

是的,我创建它通过wmd编辑器markdown并在服务器级别转换为HTML和谷歌代码美化语法突出显示,我需要添加“prettyprint”类pre标签 – Hellnar 2011-01-22 12:40:59

回答

4

使用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>