scream

Web scraping for Gleam.

Turn HTML into a Document with parse (or XML, such as an RSS or Atom feed, with parse_xml), then find what you want with CSS selectors and read it out:

let assert Ok(document) =
  scream.parse("<ul><li class=\"post\">Teaming on a cleaning bid</li></ul>")
let assert Ok(posts) = scream.select(scream.root(document), "li.post")
list.map(posts, scream.text)
// -> ["Teaming on a cleaning bid"]

To download pages politely, see scream/fetch.

Types

A parsed HTML or XML document.

pub opaque type Document

One element in a Document: the document plus where you are in it.

pub opaque type Element

Why select or select_one found nothing.

pub type SelectorError {
  InvalidSelector(selector: String, reason: String)
  NoMatch
}

Constructors

  • InvalidSelector(selector: String, reason: String)

    The selector couldn’t be read; reason says where it went wrong.

  • NoMatch

    select_one found no matching element.

Values

pub fn ancestors(element: Element) -> List(Element)

The elements this one is inside, nearest first, ending with the root.

pub fn attribute(
  element: Element,
  name: String,
) -> Result(String, Nil)

The value of one attribute, if the element has it.

pub fn attributes(element: Element) -> List(#(String, String))

All of the element’s attributes, in the order they were written.

pub fn children(element: Element) -> List(Element)

The elements directly inside this one, in document order. Text is not included: use text or own_text for that.

pub fn descendants(element: Element) -> List(Element)

Every element inside this one, at any depth, in document order.

pub fn matches(
  element: Element,
  css: String,
) -> Result(Bool, SelectorError)

Whether the element itself matches a CSS selector.

pub fn next_sibling(element: Element) -> Result(Element, Nil)

The element right after this one under the same parent.

pub fn own_text(element: Element) -> String

Only the text directly inside the element, not inside its children.

pub fn parent(element: Element) -> Result(Element, Nil)

The element this one is inside, or an error for the root.

pub fn parse(html: String) -> Result(Document, Nil)

Parse an HTML string into a Document.

HTML is parsed the way browsers do it, so missing html, head and body elements are filled in: the root is always html.

Returns an error if the HTML can’t be parsed or holds no elements.

pub fn parse_xml(xml: String) -> Result(Document, Nil)

Parse an XML string, such as an RSS or Atom feed, into a Document.

Element and attribute names are stored without their namespace prefix, so <media:thumbnail> is selected as thumbnail. Documents that declare entities are refused, and external entities are never loaded.

Feeds often carry HTML as escaped text (Atom’s <content type="html">, RSS’s <description>). text gives you that HTML back as a string, ready for parse:

let assert Ok(feed) = scream.parse_xml(atom)
let assert Ok(content) = scream.select_one(scream.root(feed), "entry content")
let assert Ok(body) = scream.parse(scream.text(content))

Returns an error if the XML is malformed.

pub fn previous_sibling(element: Element) -> Result(Element, Nil)

The element right before this one under the same parent.

pub fn root(document: Document) -> Element

The document’s outermost element. For HTML this is always html.

pub fn select(
  element: Element,
  css: String,
) -> Result(List(Element), SelectorError)

Find every element inside element that matches a CSS selector, in document order. The element itself is never included, but the selector may refer to its ancestors ("feed > entry" works from the feed’s root).

Supported: type (li), *, #id, .class, attributes ([href], [rel=alternate], ~=, ^=, $=, *=), compound selectors (a.post), the descendant, >, + and ~ combinators, groups with ,, and :first-child, :last-child, :nth-child(an+b) and :not(...).

Tag and attribute names match regardless of case; values are exact.

pub fn select_one(
  element: Element,
  css: String,
) -> Result(Element, SelectorError)

Like select, but only the first match.

pub fn tag(element: Element) -> String

The element’s tag name, such as "div", without any namespace prefix.

pub fn text(element: Element) -> String

All the text inside the element, at any depth, joined in document order.

Whitespace is kept as written; trim or collapse it as you need.

✨ Search Document