Debian Codegraph @

doc/cssselect.txt

raw
1 ==============
2 lxml.cssselect
3 ==============
4
5 lxml supports a number of interesting languages for tree traversal and element
6 selection. The most important is obviously XPath_, but there is also
7 ObjectPath_ in the `lxml.objectify`_ module. The newest child of this family
8 is `CSS selection`_, which is made available in form of the ``lxml.cssselect``
9 module.
10
11 Although it started its life in lxml, cssselect_ is now an independent project.
12 It translates CSS selectors to XPath 1.0 expressions that can be used with
13 lxml's XPath engine. ``lxml.cssselect`` adds a few convenience shortcuts into
14 that package.
15
16 To install ``cssselect``, run
17
18 ::
19
20 pip install cssselect
21
22 lxml will then import and use it automatically.
23
24
25 .. _XPath: xpathxslt.html#xpath
26 .. _ObjectPath: objectify.html#objectpath
27 .. _`lxml.objectify`: objectify.html
28 .. _`CSS selection`: http://www.w3.org/TR/CSS21/selector.html
29 .. _cssselect: http://packages.python.org/cssselect/
30
31 .. contents::
32 ..
33 1 The CSSSelector class
34 2 CSS Selectors
35 2.1 Namespaces
36 3 Limitations
37
38
39 The CSSSelector class
40 =====================
41
42 The most important class in the ``lxml.cssselect`` module is ``CSSSelector``. It
43 provides the same interface as the XPath_ class, but accepts a CSS selector
44 expression as input:
45
46 .. sourcecode:: pycon
47
48 >>> from lxml.cssselect import CSSSelector
49 >>> sel = CSSSelector('div.content')
50 >>> sel #doctest: +ELLIPSIS
51 <CSSSelector ... for 'div.content'>
52 >>> sel.css
53 'div.content'
54
55 The selector actually compiles to XPath, and you can see the
56 expression by inspecting the object:
57
58 .. sourcecode:: pycon
59
60 >>> sel.path
61 "descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' content ')]"
62
63 To use the selector, simply call it with a document or element
64 object:
65
66 .. sourcecode:: pycon
67
68 >>> from lxml.etree import fromstring
69 >>> h = fromstring('''<div id="outer">
70 ... <div id="inner" class="content body">
71 ... text
72 ... </div></div>''')
73 >>> [e.get('id') for e in sel(h)]
74 ['inner']
75
76 Using ``CSSSelector`` is equivalent to translating with ``cssselect``
77 and using the ``XPath`` class:
78
79 .. sourcecode:: pycon
80
81 >>> from cssselect import GenericTranslator
82 >>> from lxml.etree import XPath
83 >>> sel = XPath(GenericTranslator().css_to_xpath('div.content'))
84
85 ``CSSSelector`` takes a ``translator`` parameter to let you choose which
86 translator to use. It can be ``'xml'`` (the default), ``'xhtml'``, ``'html'``
87 or a `Translator object`_.
88
89 .. _Translator object: http://packages.python.org/cssselect/#cssselect.GenericTranslator
90
91
92 The cssselect method
93 ====================
94
95 lxml ``Element`` objects have a ``cssselect`` convenience method.
96
97 .. sourcecode:: pycon
98
99 >>> h.cssselect('div.content') == sel(h)
100 True
101
102 Note however that pre-compiling the expression with the ``CSSSelector`` or
103 ``XPath`` class can provide a substantial speedup.
104
105 The method also accepts a ``translator`` parameter. On ``HtmlElement``
106 objects, the default is changed to ``'html'``.
107
108
109 Supported Selectors
110 ===================
111
112 Most `Level 3`_ selectors are supported. The details are in the
113 `cssselect documentation`_.
114
115 .. _Level 3: http://www.w3.org/TR/2011/REC-css3-selectors-20110929/
116 .. _cssselect documentation: http://packages.python.org/cssselect/#supported-selectors
117
118
119 Namespaces
120 ==========
121
122 In CSS you can use ``namespace-prefix|element``, similar to
123 ``namespace-prefix:element`` in an XPath expression. In fact, it maps
124 one-to-one, and the same rules are used to map namespace prefixes to
125 namespace URIs: the ``CSSSelector`` class accepts a dictionary as its
126 ``namespaces`` argument.