Domselect
Domselect provides high-level API to work with structure of HTML document using one of HTML processing backend. To work with HTML document you have to create so-called selector object from raw content of HTML document. That selector will be bound to the root node of HTML structure. Then you can call different methods of these selector to build other selectors bound to nested parts of HTML structure.
Selector object extracts low-level nodes from DOM constructed by HTML processing backend and wraps them into high-level selector interface. If you need, you can always access low-level node stored in selector object.
Selector Backends
Domselect library provides these selectors:
-
LexborSelector powered by selectolax and lexbor libraries. The type of raw node is
selectolax.lexbor.LexborNode. Query language is CSS. Lexbor parser is x3-x4 times faster than lxml parser. -
LxmlCssSelector powered by lxml library. The type of raw node is
lxml.html.HtmlElement. Query language is CSS. -
LxmlXpathSelector powered by lxml library. The type of raw node is
lxml.html.HtmlElement. Query language is XPATH.
Selector Creating
To create lexbor selector from content of HTML document:
from domselect import LexborSelector
sel = LexborSelector.from_content("<div>test</div>")
Also you can create selector from raw node:
from domselect import LexborSelector
from selectolax.lexbor import LexborHTMLParser
node = LexborHTMLParser("<div>test</div>").css_first("div")
sel = LexborSelector(node)
Same goes for lxml backend. Here is an example of creating lxml selector from raw node:
from lxml.html import fromstring
from domselect import LxmlCssSelector, LxmlXpathSelector
node = fromstring("<div>test</div>")
sel = LxmlCssSelector(node)
# or
sel = LxmlXpathSelector(node)
Node Traversal Methods
Each of these methods return other selectors of same type i.e. LexborSelector return other LexborSelectors and LxmlCssSelector returns other LxmlCssSelectors.
Method find(query: str) returns list of selectors bound to raw nodes found by query.
Method first(query: str) returns None of selector bound to first raw node found by query.
There is similar find_raw and first_raw methods which works in same way but returns low-level raw nodes
i.e. they do not wrap found nodes into selector interface.
Method parent() returns selector bound to raw node which is parent to raw node of current selector.
Method exists(query: str) returns boolean flag indicates if any node has been found by query.
Method first_contains(query: str, pattern: str[, default: None]) returns selector bound to first raw node
found by query and which contains text as pattern parameter. If node is not found then
NodeNotFoundError is raised. You can pass default=None optional parameter to return None in case
of node is not found.
Node Properties Methods
Method attr(name: str[, default: None|str]) returns content of node's attribute of given name.
If node does not have such attribute the AttributeNotFoundError is raised. If you pass optional
default: None|str parameter the method will return None or str if attribute does not exists.
Method text([strip: bool]) returns text content of current node and all its sub-nodes. By default
returned text is stripped at beginning and ending from whitespaces, tabulations and line-breaks. You
can turn off striping by passing strip=False parameter.
Method tag() returns tag name of raw node to which current selector is bound.
Traversal and Properties Methods
These methods combine two operations: search node by query and do something on found node. They are helful
if you want to get text or attribute from found node, but this node might not exist. Such methods allows you
to return reasonable default value in case node is not found. On contrary, if you use call chain like first().text()
then you'll not be able to return default value from text() call because first() will raise Exception if
node is not found.
Method first_attr(query: str, name: str[, default: None|str]) returns content of attribute of given name of node
found by given query. If node does not have such attribute the AttributeNotFoundError is raised.
If node is not found by given query the NodeNotFoundError is raised. If you pass optional
default: None|str parameter the method will return None or str instead of rasing exceptions.
Method first_text(query: str[, default: None|str, strip: bool]) returns text content of raw node (and all its
sub-nodes) found by given query. If node is not found the NodeNotFoundError is raised. Use optional default: None|str
parametere to return None or str instead of raising exceptions. You can control text stripping with strip
parameter (see description of text() method).
Usage example
This code downloads telegram channel preview page and parse external links from it.
from html import unescape
from urllib.request import urlopen
from domselect import LexborSelector
def main() -> None:
content = urlopen("https://t.me/s/centralbank_russia").read()
sel = LexborSelector.from_content(content)
for msg_node in sel.find(".tgme_widget_message_wrap"):
msg_date = msg_node.first_attr(
".tgme_widget_message_date time", "datetime", default=None
)
for text_node in msg_node.find(".tgme_widget_message_text"):
print("Message by {}".format(msg_date))
for link_node in text_node.find("a[href]"):
url = link_node.attr("href")
if url.startswith("http"):
print(" - {}".format(unescape(url)))
if __name__ == "__main__":
main()
Metadata
Release files for domselect 0.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| domselect-0.0.6.tar.gz | 11.7 kB | Without provenance | Details for domselect-0.0.6.tar.gz |
Release files / domselect-0.0.6.tar.gz
| Download URL | domselect-0.0.6.tar.gz |
|---|---|
| Size | 11.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
514a1817ee26c481392759c3d4f1ebaef5b1b6804a75d83aa9592ca2f214bcab
|
|
BLAKE2b-256 checksum How to use checksums |
b1f682236af357002e593d8590d7b38954dcd3738db7d36f24e3eb5e5043486e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.0
|
Security for domselect 0.0.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.