Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python lxml find <fb:comments /> tag

Tags:

python

lxml

I'm using lxml to parse an html that has a facebook comments tag that looks like that:

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

I am trying to select it to get the href value but when i do a cssselect('fb:comments') i get the following error:

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

Is there a way to do it?

Edit: The code:

from lxml.html import fromstring
html = '...'
parser = fromstring(html)
parser.cssselect('fb:comments')  #raises the exception 
like image 526
applechief Avatar asked Sep 22 '26 18:09

applechief


1 Answers

The cssselect() method parses the document using given CSS selector expression. In your case the colon character (:) is a XML namespace prefix separator (i.e. <namespace:tagname/>) which is confused with CSS pseudo-class syntax (i.e. tagname:pseudo-class).

According to lxml manual you should use namespace-prefix|element syntax in cssselect()in order to to find a tag (comments) with a namespace prefix (fb). So:

from lxml.html import fromstring
html = '...'
parser = fromstring(html)
parser.cssselect('fb|comments')
like image 143
Mariusz Jamro Avatar answered Sep 25 '26 06:09

Mariusz Jamro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!