]
26 |
27 | Breaking out of a level of traversal is also supported using end::
28 |
29 | >>> d('p').find('a').end()
30 | [,
]
31 | >>> d('p').eq(0).end()
32 | [
,
]
33 | >>> d('p').filter(lambda i: i == 1).end()
34 | [
,
]
35 |
36 |
37 | If you want to select a dotted id you need to escape the dot::
38 |
39 | >>> d = pq('
')
40 | >>> d(r'#hello\.you')
41 | []
42 |
43 |
--------------------------------------------------------------------------------
/pyquery/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (C) 2008 - Olivier Lauzanne
2 | #
3 | # Distributed under the BSD license, see LICENSE.txt
4 |
5 | from .pyquery import PyQuery # NOQA
6 |
--------------------------------------------------------------------------------
/pyquery/cssselectpatch.py:
--------------------------------------------------------------------------------
1 | # Copyright (C) 2008 - Olivier Lauzanne
2 | #
3 | # Distributed under the BSD license, see LICENSE.txt
4 | from __future__ import unicode_literals
5 | from cssselect import xpath as cssselect_xpath
6 | from cssselect.xpath import ExpressionError
7 |
8 | XPathExprOrig = cssselect_xpath.XPathExpr
9 |
10 |
11 | class XPathExpr(XPathExprOrig):
12 |
13 | def __init__(self, path='', element='*', condition='', star_prefix=False):
14 | self.path = path
15 | self.element = element
16 | self.condition = condition
17 | self.post_condition = None
18 |
19 | def add_post_condition(self, post_condition):
20 | if self.post_condition:
21 | self.post_condition = '%s and (%s)' % (self.post_condition,
22 | post_condition)
23 | else:
24 | self.post_condition = post_condition
25 |
26 | def __str__(self):
27 | path = XPathExprOrig.__str__(self)
28 | if self.post_condition:
29 | path = '%s[%s]' % (path, self.post_condition)
30 | return path
31 |
32 | def join(self, combiner, other,
33 | closing_combiner=None, has_inner_condition=False):
34 | res = XPathExprOrig.join(self, combiner, other,
35 | closing_combiner=closing_combiner,
36 | has_inner_condition=has_inner_condition)
37 | self.post_condition = other.post_condition
38 | return res
39 |
40 |
41 | # keep cssselect < 0.8 compat for now
42 |
43 |
44 | class JQueryTranslator(cssselect_xpath.HTMLTranslator):
45 | """This class is used to implement the css pseudo classes
46 | (:first, :last, ...) that are not defined in the css standard,
47 | but are defined in the jquery API.
48 | """
49 |
50 | xpathexpr_cls = XPathExpr
51 |
52 | def xpath_first_pseudo(self, xpath):
53 | """Matches the first selected element::
54 |
55 | >>> from pyquery import PyQuery
56 | >>> d = PyQuery('')
57 | >>> d('p:first')
58 | []
59 |
60 | ..
61 | """
62 | xpath.add_post_condition('position() = 1')
63 | return xpath
64 |
65 | def xpath_last_pseudo(self, xpath):
66 | """Matches the last selected element::
67 |
68 | >>> from pyquery import PyQuery
69 | >>> d = PyQuery('')
70 | >>> d('p:last')
71 | []
72 |
73 | ..
74 | """
75 | xpath.add_post_condition('position() = last()')
76 | return xpath
77 |
78 | def xpath_even_pseudo(self, xpath):
79 | """Matches even elements, zero-indexed::
80 |
81 | >>> from pyquery import PyQuery
82 | >>> d = PyQuery('')
83 | >>> d('p:even')
84 | []
85 |
86 | ..
87 | """
88 | # the first element is 1 in xpath and 0 in python and js
89 | xpath.add_post_condition('position() mod 2 = 1')
90 | return xpath
91 |
92 | def xpath_odd_pseudo(self, xpath):
93 | """Matches odd elements, zero-indexed::
94 |
95 | >>> from pyquery import PyQuery
96 | >>> d = PyQuery('
')
97 | >>> d('p:odd')
98 | []
99 |
100 | ..
101 | """
102 | xpath.add_post_condition('position() mod 2 = 0')
103 | return xpath
104 |
105 | def xpath_checked_pseudo(self, xpath):
106 | """Matches odd elements, zero-indexed::
107 |
108 | >>> from pyquery import PyQuery
109 | >>> d = PyQuery('')
110 | >>> d('input:checked')
111 | []
112 |
113 | ..
114 | """
115 | xpath.add_condition("@checked and name(.) = 'input'")
116 | return xpath
117 |
118 | def xpath_selected_pseudo(self, xpath):
119 | """Matches all elements that are selected::
120 |
121 | >>> from pyquery import PyQuery
122 | >>> d = PyQuery('')
123 | >>> d('option:selected')
124 | [