Pyjo.DOM.CSS - CSS selector engine¶
import Pyjo.DOM.CSS
# Select elements from DOM tree
css = Pyjo.DOM.CSS.new(tree=tree)
elements = css.select('head > title')
Pyjo.DOM.CSS
is the CSS selector engine used by Pyjo.DOM
and based on
Selectors Level 3.
Selectors¶
All CSS selectors that make sense for a standalone parser are supported.
E[foo=”bar”]¶
An E
element whose foo
attribute value is exactly equal to bar
.
case_sensitive = css.select('input[type="hidden"]')
case_sensitive = css.select('input[type=hidden]')
E[foo=”bar” i]¶
An E
element whose foo
attribute value is exactly equal to any
(ASCII-range) case-permutation of bar
. Note that this selector is
EXPERIMENTAL and might change without warning!
case_insensitive = css.select('input[type="hidden" i]')
case_insensitive = css.select('input[type=hidden i]')
case_insensitive = css.select('input[class~="foo" i]')
This selector is part of Selectors Level 4, which is still a work in progress.
E[foo~=”bar”]¶
An E
element whose foo
attribute value is a list of
whitespace-separated values, one of which is exactly equal to bar
.
foo = css.select('input[class~="foo"]')
foo = css.select('input[class~=foo]')
E[foo^=”bar”]¶
An E
element whose foo
attribute value begins exactly with the string
bar
.
begins_with = css.select('input[name^="f"]')
begins_with = css.select('input[name^=f]')
E[foo$=”bar”]¶
An E
element whose foo
attribute value ends exactly with the string
bar
.
ends_with = css.select('input[name$="o"]')
ends_with = css.select('input[name$=o]')
E[foo*=”bar”]¶
An E
element whose foo
attribute value contains the substring bar
.
contains = css.select('input[name*="fo"]')
contains = css.select('input[name*=fo]')
E:checked¶
A user interface element E
which is checked (for instance a radio-button or
checkbox).
input = css.select(':checked')
E:nth-child(n)¶
An E
element, the n-th
child of its parent.
third = css.select('div:nth-child(3)')
odd = css.select('div:nth-child(odd)')
even = css.select('div:nth-child(even)')
top3 = css.select('div:nth-child(-n+3)')
E:nth-last-child(n)¶
An E
element, the n-th
child of its parent, counting from the last one.
third = css.select('div:nth-last-child(3)')
odd = css.select('div:nth-last-child(odd)')
even = css.select('div:nth-last-child(even)')
bottom3 = css.select('div:nth-last-child(-n+3)')
E:nth-of-type(n)¶
An E
element, the n-th
sibling of its type.
third = css.select('div:nth-of-type(3)')
odd = css.select('div:nth-of-type(odd)')
even = css.select('div:nth-of-type(even)')
top3 = css.select('div:nth-of-type(-n+3)')
E:nth-last-of-type(n)¶
An E
element, the n-th
sibling of its type, counting from the last one.
third = css.select('div:nth-last-of-type(3)')
odd = css.select('div:nth-last-of-type(odd)')
even = css.select('div:nth-last-of-type(even)')
bottom3 = css.select('div:nth-last-of-type(-n+3)')
E:not(s)¶
An E
element that does not match simple selector s
.
others = css.select('div p:not(:first-child)')
E[foo=bar][bar=baz]¶
An E
element whose attributes match all following attribute selectors.
links = css.select('a[foo^=b][foo$=ar]')
Classes¶
-
class
Pyjo.DOM.CSS.
Pyjo_DOM_CSS
(**kwargs)¶ Pyjo.DOM.CSS
inherits all attributes and methods fromPyjo.Base
and implements the following new ones.-
matches
(pattern)¶ bool = css.matches('head > title')
Check if first node in
tree
matches the CSS selector.
-
select_one
(pattern)¶ results = css.select_one('head > title')
Run CSS selector against
tree
and stop as soon as the first node matched.
-
tree
= None¶ tree = html.tree html.tree = ['root']
Document Object Model. Note that this structure should only be used very carefully since it is very dynamic.
-
-
Pyjo.DOM.CSS.
object
¶ alias of
Pyjo_DOM_CSS