Packages

Web scraping for Gleam, inspired by Scrapling.

Current section

Files

Jump to
scream README.md
Raw

README.md

# scream

[![Package Version](https://img.shields.io/hexpm/v/scream)](https://hex.pm/packages/scream)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://scream.hexdocs.pm/)

Web scraping for Gleam, inspired by Scrapling: fetch pages politely, parse
HTML or XML, find what you want with CSS selectors, and read it out. The API
will change before 1.0.

```sh
gleam add scream
```

```gleam
import gleam/list
import scream
import scream/fetch

pub fn main() {
  // Say who you are: sites rate limit and block anonymous clients first.
  let fetcher = fetch.new("my-research-bot/1.0 (+https://example.com/bot)")

  // Feeds are XML. The fetcher waits between requests and backs off on 429.
  let assert Ok(response) =
    fetch.get(fetcher, "https://example.com/feed.atom")
  let assert Ok(feed) = scream.parse_xml(response.body)

  let assert Ok(titles) = scream.select(scream.root(feed), "entry > title")
  list.map(titles, scream.text)
}
```

- `scream.parse` / `scream.parse_xml` turn a string into a `Document`.
- `scream.select`, `select_one` and `matches` take CSS selectors: types,
  `#id`, `.class`, attribute selectors, the four combinators, `,` groups,
  `:first-child`, `:last-child`, `:nth-child()` and `:not()`.
- `root`, `parent`, `children`, `next_sibling`, `previous_sibling`,
  `ancestors` and `descendants` move around the tree; `tag`, `attribute`,
  `attributes`, `text` and `own_text` read from it.
- `scream/fetch` makes GET requests with your User-Agent and headers, a delay
  between requests, and retries that honor `Retry-After`. For JSON APIs, decode
  `response.body` with `gleam_json`.

Further documentation can be found at <https://scream.hexdocs.pm/>.

## Development

scream runs on the Erlang target. Its HTML parser, htmerl, is built with
rebar3, so have rebar3 on your PATH.

```sh
gleam test  # Run the tests
```