Translate CSS Selectors to XPath Expressions

Translates a CSS selector into an equivalent XPath expression. This allows us to use CSS selectors when working with the 'XML' and 'xml2' packages, which can only evaluate XPath expressions. Also provided are convenience functions for querying XML and HTML documents with CSS selectors. This package was originally a port of the Python package 'cssselect' (< https://cssselect.readthedocs.io/>).


selectr

License (3-Clause BSD) GitHub Actions CRAN version codecov Downloads per month

selectr is a package which makes working with HTML and XML documents easier. It does this by performing translation of CSS selectors into XPath expressions so that you can query XML and xml2 documents easily.

library(selectr)
xpath <- css_to_xpath("#selectr")
xpath
#> [1] "descendant-or-self::*[@id = 'selectr']"

Installation

Install the release version from CRAN

install.packages("selectr")

Install the development version from GitHub

# install.packages("remotes")
remotes::install_github("sjp/selectr")

Overview

The key functions in selectr are:

  • Translate a CSS selector into an XPath expression with css_to_xpath().

  • Query an XML or xml2 document with querySelector() and its variants.

    • Find the first matching node with querySelector().

    • Find all matching nodes with querySelectorAll().

    • Find the first matching node in a namespaced document with querySelectorNS().

    • Find all matching nodes in a namespaced document with querySelectorAllNS().

Examples

Scraping an HTML document

library(selectr)
library(xml2)

html <- paste0(
  "<html><body>",
  "<table id='products'>",
  "<tr><td class='name'>Widget</td><td class='price'>9.99</td></tr>",
  "<tr><td class='name'>Gadget</td><td class='price'>19.99</td></tr>",
  "</table>",
  "<input type='checkbox' checked>",
  "</body></html>")
doc <- read_html(html)

# :has() picks out the table, chaining then walks its rows
rows <- querySelectorAll(doc, "table:has(.price) tr")
querySelectorAll(rows, "td.name")
#> {xml_nodeset (2)}
#> [1] <td class="name">Widget</td>
#> [2] <td class="name">Gadget</td>

# The html translator (auto-detected from read_html()) implements :checked
querySelector(doc, "input:checked")
#> {html_node}
#> <input type="checkbox" checked="checked">

Querying a namespaced XML document

library(selectr)
library(xml2)

# A document with both SVG and MathML content
svgdoc <- read_xml(system.file("demos/svg-mathml.svg", package = "selectr"))
querySelectorAllNS(svgdoc, "svg|script, math|mo",
                   c(svg = "http://www.w3.org/2000/svg",
                     math = "http://www.w3.org/1998/Math/MathML"))

Development

The Makefile wraps the usual tasks:

  • make test runs the test suite (testthat::test_local()).
  • make lint lints the package (configured by .lintr).
  • make check builds the source tarball and runs a full CRAN-style R CMD check --as-cran on it (make build only builds the tarball).

A devcontainer is provided with R and the package's dependencies preinstalled.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("selectr")

0.8-1 by Simon Potter, 3 days ago


https://sjp.co.nz/projects/selectr/


Report a bug at https://github.com/sjp/selectr/issues


Browse source code at https://github.com/cran/selectr


Authors: Simon Potter [aut, trl, cre] , Simon Sapin [aut] , Ian Bicking [aut]


Documentation:   PDF Manual  


BSD_3_clause + file LICENCE license


Suggests testthat, XML, xml2


Imported by cliapp, rvest, wikilake.

Suggested by RSelenium, ggpedigree, unpivotr.


See at CRAN