cssselect

CSS Selectors based on XPath.

This module supports selecting XML/HTML tags based on CSS selectors. See the CSSSelector class for details.

This is a thin wrapper around cssselect 0.7 or later.

Module Contents

Classes

LxmlTranslator() A custom CSS selector to XPath translator with lxml-specific extensions.
LxmlHTMLTranslator() lxml extensions + HTML support.
CSSSelector(self,css,namespaces=None,translator=”xml”) A CSS selector.

Functions

_make_lower_case(context,s)
class LxmlTranslator

A custom CSS selector to XPath translator with lxml-specific extensions.

xpath_contains_function(xpath, function)
class LxmlHTMLTranslator

lxml extensions + HTML support.

_make_lower_case(context, s)
class CSSSelector(css, namespaces=None, translator="xml")

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')]
__init__(css, namespaces=None, translator="xml")
__repr__()