2012-06-19 17 views
3

我使用lxml的解析,有一個Facebook的評論標籤,看起來像在HTML:蟒蛇LXML找到<FB:評論/>標籤

<fb:comments id="fb_comments" href="http://example.com" num_posts="5" width="600"></fb:comments> 

我想選擇它來獲得HREF值,但是當我做了cssselect('fb:comments')我得到以下錯誤:

The pseudo-class Symbol(u'comments', 3) is unknown 

有沒有辦法做到這一點?

編輯: 代碼:

from lxml.html import fromstring 
html = '...' 
parser = fromstring(html) 
parser.cssselect('fb:comments') #raises the exception 
+0

請提供您的python代碼。 –

+0

@Secator代碼加入 – applechief

回答

3

cssselect()方法解析使用給定CSS selector表達該文檔。在你的情況下,冒號(:)是一個與CSS僞類語法(即tagname:pseudo-class)混淆的XML命名空間前綴分隔符(即<namespace:tagname/>)。

根據lxml manual你應該爲了找到一個命名空間前綴(fb)標籤(comments)使用cssselect()namespace-prefix|element語法。所以:

from lxml.html import fromstring 
html = '...' 
parser = fromstring(html) 
parser.cssselect('fb|comments') 
+0

太棒了!謝謝, 但這是返回'未定義名稱空間前綴'任何想法爲什麼? – applechief

+1

也許只用'comments'(w/o命名空間)試試吧? –

+0

是的!有效。謝謝:) – applechief