API reference

class simdsel.parsel.Selector(text=None, type=None, body=None, encoding=None, namespaces=None, root=None, base_url=None, _expr=None, huge_tree=None, mode=None)

A parsel-shaped selector over one HTML document or one result.

Create a document selector with Selector(text=...) or Selector(body=...), then chain css() and xpath():

`python >>> from simdsel.parsel import Selector >>> sel = Selector(text="<p>hi</p>") >>> sel.css("p::text").get() 'hi' `

Queries return a SelectorList; get() / getall() turn it into strings. Documents are immutable and shared by chained selectors.

Parameters:
  • text (str) – Already decoded HTML.

  • type (str) – "html" (default) or "text".

  • body (bytes) – HTML bytes; the charset is sniffed unless encoding is given.

  • encoding (str) – Charset label for body= or text=.

  • namespaces (dict) – XPath prefix map (stored, not resolved).

  • root – Not supported; an lxml node cannot be shared.

  • base_url (str) – Stored as metadata and reported by base_url.

  • _expr (str) – The query that produced this selector, for repr.

  • huge_tree (bool) – Accepted for parsel compatibility; ignored.

  • mode (str) – "none" (default, fast flat parse) or "spec" (HTML-spec tree construction).

Raises:
  • ValueError – If no input is given, or type / mode / encoding is invalid.

  • NotImplementedError – For type="xml" / "json" or root=.

attrib

The attributes of the first element item.

Returns:

dict[str, str]

Raises:

AttributeError – If the first item is a string.

base_url

The stored base URL, if any. simdsel never fetches anything.

css(query)

Compiles and evaluates a CSS selector inside the current items.

The current item itself is not matched; use :scope to include it. ::text and ::attr(name) select text and attribute values.

Parameters:

query (str) – A CSS selector.

Returns:

One selector per match, in document order.

Return type:

SelectorList

Raises:

ValueError – If query does not parse.

drop()

Not supported: documents are immutable.

Raises:

NotImplementedError – Always.

extract()

Alias for get().

extract_first()

Alias for get().

get()

The serialized value of the first item.

For an element this is the original source slice, not a re-serialization. Returns None when there is no first item.

Returns:

str | None

getall()

The serialized values of all items.

Returns:

One value per item (a one-element list for a Selector).

Return type:

list[str]

namespaces

Namespace prefixes reported to xpath() (parsel’s EXSLT defaults).

static parse_many(bodies)

Parses a list of byte buffers into document selectors.

Parameters:

bodies (Iterable[bytes]) – HTML byte buffers.

Returns:

list[Selector]

re(pattern, replace_entities=True)

Applies a Python re pattern to get() and returns all matches.

The pattern language is CPython’s re. A named group extract takes precedence; otherwise findall is flattened.

Parameters:
  • pattern (str | re.Pattern) – A pattern or a compiled pattern.

  • replace_entities (bool) – Decode character references except &amp; and &lt;.

Returns:

list[str]

re_first(pattern, default=None, replace_entities=True)

Returns the first match of re(), or default.

Parameters:
  • pattern (str | re.Pattern) – A pattern or a compiled pattern.

  • default (str) – Value returned when there is no match.

  • replace_entities (bool) – As in re().

Returns:

str | None

root

simdsel has no lxml node.

Raises:

NotImplementedError – Always.

Type:

Not supported

selectorlist_cls

The class returned by css() and xpath().

text

The text content of the first item.

Elements return their subtree text; text items return themselves.

Returns:

str | None

type

"html" or "text".

Type:

parsel’s Selector.type

xpath(query, namespaces=None, **kwargs)

Evaluates an XPath 1.0 expression.

Node-sets become element or text selectors; scalars become string selectors. Namespace prefixes are accepted but not resolved (the DOM stores local names), and variables are not supported.

Parameters:
  • query (str) – An XPath 1.0 expression.

  • namespaces (dict) – Accepted for parsel compatibility.

  • **kwargs – XPath variables; passing any raises.

Returns:

One selector per node or scalar.

Return type:

SelectorList

Raises:
  • ValueError – If query does not parse or evaluate.

  • NotImplementedError – If variables are passed.

class simdsel.parsel.SelectorList

A parsel-shaped list of selectors.

Selector.css() and Selector.xpath() apply the query to every item and flatten the results. The list is mutable and supports indexing, slicing, iteration, len() and +.

`python >>> sel = Selector(text="<b>a</b><b>b</b>") >>> [item.get() for item in sel.css("b")] ['<b>a</b>', '<b>b</b>'] `

append(item)

Appends a selector to the list.

attrib

The attributes of the first item.

Returns:

dict[str, str]

Raises:

AttributeError – If the first item is a string.

clear()

Removes every item.

css(query)

Applies a CSS selector to every item and flattens the result.

Parameters:

query (str) – A CSS selector.

Returns:

SelectorList

Raises:

ValueError – If query does not parse.

drop()

Not supported: documents are immutable.

Raises:

NotImplementedError – Always.

extend(other)

Appends every item of other to the list.

extract()

Alias for getall().

extract_first(default=None)

Alias for get().

get(default=None)

The first item’s value, or default when the list is empty.

Parameters:

default (str) – Value returned for an empty list.

Returns:

str | None

getall()

All items’ values.

Returns:

list[str]

insert(index, item)

Inserts a selector at index.

pop(index=Ellipsis)

Removes and returns the selector at index (default: last).

re(pattern, replace_entities=True)

Applies a regex to every item’s value and flattens the matches.

Parameters:
  • pattern (str | re.Pattern) – A pattern or a compiled pattern.

  • replace_entities (bool) – Decode character references except &amp; and &lt;.

Returns:

list[str]

re_first(pattern, default=None, replace_entities=True)

The first match across items, or default.

Parameters:
  • pattern (str | re.Pattern) – A pattern or a compiled pattern.

  • default (str) – Value returned when there is no match.

  • replace_entities (bool) – As in re().

Returns:

str | None

text

The text content of every item.

Returns:

list[str]

xpath(query, namespaces=None, **kwargs)

Evaluates an XPath expression on every item and flattens the result.

Parameters:
  • query (str) – An XPath 1.0 expression.

  • namespaces (dict) – Accepted for parsel compatibility.

  • **kwargs – XPath variables; passing any raises.

Returns:

SelectorList

Raises:
  • ValueError – If query does not parse or evaluate.

  • NotImplementedError – If variables are passed.