lxml.cssselect.CSSSelector
Package lxml :: Module cssselect :: Class CSSSelector
[hide private]
[frames] | no frames]

Class CSSSelector

source code

               object --+        
                        |        
etree._XPathEvaluatorBase --+    
                            |    
                  etree.XPath --+
                                |
                               CSSSelector

A CSS selector.

Usage:

>>> from lxml import etree, cssselect
>>> select = cssselect.CSSSelector("a tag > child")

>>> root = etree.XML("<a><b><c/><tag><child>TEXT</child></tag></b></a>")
>>> [ el.tag for el in select(root) ]
['child']

To use CSS namespaces, you need to pass a prefix-to-namespace mapping as namespaces keyword argument:

>>> rdfns = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
>>> select_ns = cssselect.CSSSelector('root > rdf|Description',
...                                   namespaces={'rdf': rdfns})

>>> rdf = etree.XML((
...     '<root xmlns:rdf="%s">'
...       '<rdf:Description>blah</rdf:Description>'
...     '</root>') % rdfns)
>>> [(el.tag, el.text) for el in select_ns(rdf)]
[('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}Description', 'blah')]
Instance Methods [hide private]
 
__init__(self, css, namespaces=None, translator='xml')
x.__init__(...) initializes x; see help(type(x)) for signature
source code
 
__repr__(self)
repr(x)
source code

Inherited from etree.XPath: __call__, __new__

Inherited from etree._XPathEvaluatorBase: evaluate

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from etree.XPath: path

Inherited from etree._XPathEvaluatorBase: error_log

Inherited from object: __class__

Method Details [hide private]

__init__(self, css, namespaces=None, translator='xml')
(Constructor)

source code 
x.__init__(...) initializes x; see help(type(x)) for signature
Overrides: object.__init__
(inherited documentation)

__repr__(self)
(Representation operator)

source code 
repr(x)
Overrides: object.__repr__
(inherited documentation)