Reaver provides an API to parse HTML and extract specific elements into Clojure data structures (EDN).
Note: Reaver does not handle HTTP fetching. You must use a library like slurp, aleph, or clj-http to retrieve the HTML content first.
To extract data, you typically follow this workflow:
- Parse the HTML string using
parse. - Use
extract-from to define selectors and the mapping of data to extract.
Commonly used functions:
parse: Converts an HTML string into a searchable document.extract-from: The primary function for pulling structured data out of a parsed document using CSS selectors.text: A helper to extract the text content of a selected element.attr: A helper to extract the value of a specific attribute (e.g., :href).
(require '[reaver :refer [parse extract-from text attr]])
; 1. Fetch the HTML (using slurp as an example)
(def html-content (slurp "https://example.com/"))
; 2. Parse and extract data
; The syntax for extract-from allows you to specify a base selector,
; then a sequence of key-value pairs where each pair consists of
; a selector and an extraction function (like `text` or `attr`).
(extract-from (parse html-content)
".item-container"
[:title :link]
".title" text
".title" (attr :href))
; Result: ({:title "...", :link "..."}, ...)