2011-01-22 73 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>