1
2
3
defmodule Meeseeks do alias Meeseeks.{Context, Document, Error, Parser, Result, Select, Selector, TupleTree} @moduledoc """ Meeseeks is an Elixir library for parsing and extracting data from HTML and XML with CSS or XPath selectors. ```elixir import Meeseeks.CSS html = HTTPoison.get!("https://news.ycombinator.com/").body for story <- Meeseeks.all(html, css("tr.athing")) do title = Meeseeks.one(story, css(".title a")) %{title: Meeseeks.text(title), url: Meeseeks.attr(title, "href")} end #=> [%{title: "...", url: "..."}, %{title: "...", url: "..."}, ...] ``` ## Features - Friendly API - Browser-grade HTML5 parser - Permissive XML parser - CSS and XPath selectors - Rich, extensible selector architecture - Helpers to extract data from selections ## Why? Meeseeks exists in the same space as an earlier library called Floki, so why was Meeseeks created and why would you use it instead of Floki? #### Floki is a couple years older than Meeseeks, so why does Meeseeks even exist? Meeseeks exists because Floki used to be unable to do what I needed. When I started learning Elixir I reimplemented a small project I had written in another language. Part of that project involved extracting data from HTML, and unbeknownst to me some of the HTML I needed to extract data from was malformed. This had never been a problem before because the HTML parser I was using in the other language was HTML5 spec compliant and handled the malformed HTML just as well as a browser. Unfortunately for me, Floki used (and still uses by default) the `:mochiweb_html` parser which is nowhere near HTML5 spec compliant, and just silently dropped the data I needed when parsing. Meeseeks started out as an attempt to write an HTML5 spec compliant parser in Elixir (spoiler: it's really hard), then switched to using Mozilla's [html5ever](https://github.com/servo/html5ever) via Rustler after [Hans](https://github.com/hansihe) wrote `html5ever_elixir`. Floki gained optional support for using `html5ever_elixir` as its parser around the same time, but it still used `:mochiweb_html` (which doesn't require Rust to be part of the build process) by default and I released Meeseeks as a safer alternative. #### Why should I use Meeseeks instead of Floki? When Meeseeks was released it came with a safer default HTML parser, a more complete collection of CSS selectors, and a more extensible selector architecture than Floki. Since then Meeseeks has been further expanded with functionality Floki just doesn't have, such as an XML parser and XPath selectors. It won't matter to most users, but the selection architecture is much richer than Floki's, and permits the creation all kinds of interesting custom, stateful selectors (in fact, both the CSS and XPath selector strings compile down to the same selector structs that anybody can define). What probably will matter more to users is the friendly API, extensive documentation, and the attention to the details of usability seen in such places as the custom formatting for result structs (`#Meeseeks.Result<{
1
}>`) and the descriptive errors. #### Is Floki ever a better choice than Meeseeks? Yes, there are two main cases when Floki is clearly a better choice than Meeseeks. Firstly, if you absolutely can't include Rust in your build process AND you know that the HTML you'll be working with is well-formed and won't require an HTML5 spec compliant parser then using Floki with the `:mochiweb_html` parser is a reasonable choice. However, if you have any doubts about the HTML you'll be parsing you should probably figure out a way to use a better parser because using `:mochiweb_html` in that situation may be a timebomb. Secondly, if you want to make updates to an HTML document then Floki provides facilities to do so while Meeseeks, which is entirely focused on selecting and extracting data, does not. #### How does performance compare between Floki and Meeseeks? Performance is similar enough between the two that it's probably not worth choosing one over the other for that reason. For details and benchmarks, see [Meeseeks vs. Floki Performance ](https://github.com/mischov/meeseeks_floki_bench). ## Compatibility Meeseeks is tested with a minimum combination of Elixir 1.4.0 and Erlang/OTP 19.3, and a maximum combination of Elixir 1.8.1 and Erlang/OTP 21.0. ## Dependencies Meeseeks depends on [html5ever](https://github.com/servo/html5ever) via [meeseeks_html5ever](https://github.com/mischov/meeseeks_html5ever). Because html5ever is a Rust library, you will need to have the Rust compiler [installed](https://www.rust-lang.org/en-US/install.html). This dependency is necessary because there are no HTML5 spec compliant parsers written in Elixir/Erlang. ## Getting Started ### Parse Start by parsing a source (HTML/XML string or `Meeseeks.TupleTree`) into a `Meeseeks.Document` so that it can be queried. `Meeseeks.parse/1` parses the source as HTML, but `Meeseeks.parse/2` accepts a second argument of either `:html` or `:xml` that specifies how the source is parsed. ```elixir document = Meeseeks.parse("1
2
3
1
}> import Meeseeks.XPath result = Meeseeks.one(document, xpath("//*[@id='main']//p")) #=> #Meeseeks.Result<{1
}> ``` ### Extract Retrieve information from the `Meeseeks.Result` with an extraction function. The extraction functions are `attr`, `attrs`, `data`, `dataset`, `html`, `own_text`, `tag`, `text`, `tree`. ```elixir Meeseeks.tag(result) #=> "p" Meeseeks.text(result) #=> "1" Meeseeks.tree(result) #=> {"p", [], ["1"]} ``` The extraction functions `html` and `tree` work on `Meeseeks.Document`s in addition to `Meeseeks.Result`s. ```elixir Meeseeks.html(document) #=> "1
2
3
Hello, Meeseeks!
1
2
3
1
}> """ @spec fetch_all(queryable, selectors) :: {:ok, [Result.t()]} | {:error, Error.t()} def fetch_all(queryable, selectors) do fetch_all(queryable, selectors, %{}) end @spec fetch_all(queryable, selectors, Context.t()) :: {:ok, [Result.t()]} | {:error, Error.t()} def fetch_all(queryable, selectors, context) def fetch_all({:error, _} = error, _selectors, _context), do: error def fetch_all(%Document{} = queryable, selectors, context) do Select.fetch_all(queryable, selectors, context) end def fetch_all(%Result{} = queryable, selectors, context) do Select.fetch_all(queryable, selectors, context) end def fetch_all(source, selectors, context) do case parse(source) do {:error, reason} -> {:error, reason} document -> Select.fetch_all(document, selectors, context) end end @doc """ Returns `[Result, ...]` if one or more nodes in the queryable match a selector, or `[]` if none do. Optionally accepts a `Meeseeks.Context` map. Parses the source if it is not a `Meeseeks.Document` or `Meeseeks.Result`, and may return `{:error, %Meeseeks.Error{type: parser}` if there is a parse error. If multiple selections are being ran on the same unparsed source, parse first to avoid unnecessary computation. ## Examples iex> import Meeseeks.CSS iex> Meeseeks.all("1
2
3
1
}> """ @spec all(queryable, selectors) :: [Result.t()] | {:error, Error.t()} def all(queryable, selectors) do all(queryable, selectors, %{}) end @spec all(queryable, selectors, Context.t()) :: [Result.t()] | {:error, Error.t()} def all(queryable, selectors, context) def all({:error, _} = error, _selectors, _context), do: error def all(%Document{} = queryable, selectors, context) do Select.all(queryable, selectors, context) end def all(%Result{} = queryable, selectors, context) do Select.all(queryable, selectors, context) end def all(source, selectors, context) do case parse(source) do {:error, reason} -> {:error, reason} document -> Select.all(document, selectors, context) end end @doc """ Returns `{:ok, Result}` for the first node in the queryable (depth-first) matching a selector, or `{:error, %Meeseeks.Error{type: :select, reason: :no_match}}` if none do. Optionally accepts a `Meeseeks.Context` map. Parses the source if it is not a `Meeseeks.Document` or `Meeseeks.Result`, and may return `{:error, %Meeseeks.Error{type: parser}` if there is a parse error. If multiple selections are being ran on the same unparsed source, parse first to avoid unnecessary computation. ## Examples iex> import Meeseeks.CSS iex> Meeseeks.fetch_one("1
2
3
1
}> """ @spec fetch_one(queryable, selectors) :: {:ok, Result.t()} | {:error, Error.t()} def fetch_one(queryable, selectors) do fetch_one(queryable, selectors, %{}) end @spec fetch_one(queryable, selectors, Context.t()) :: {:ok, Result.t()} | {:error, Error.t()} def fetch_one(queryable, selectors, context) def fetch_one({:error, _} = error, _selectors, _context), do: error def fetch_one(%Document{} = queryable, selectors, context) do Select.fetch_one(queryable, selectors, context) end def fetch_one(%Result{} = queryable, selectors, context) do Select.fetch_one(queryable, selectors, context) end def fetch_one(source, selectors, context) do case parse(source) do {:error, reason} -> {:error, reason} document -> Select.fetch_one(document, selectors, context) end end @doc """ Returns a `Result` for the first node in the queryable (depth-first) matching a selector, or `nil` if none do. Optionally accepts a `Meeseeks.Context` map. Parses the source if it is not a `Meeseeks.Document` or `Meeseeks.Result`, and may return `{:error, %Meeseeks.Error{type: parser}` if there is a parse error. If multiple selections are being ran on the same unparsed source, parse first to avoid unnecessary computation. ## Examples iex> import Meeseeks.CSS iex> Meeseeks.one("1
2
3
1
}> """ @spec one(queryable, selectors) :: Result.t() | nil | {:error, Error.t()} def one(queryable, selectors) do one(queryable, selectors, %{}) end @spec one(queryable, selectors, Context.t()) :: Result.t() | nil | {:error, Error.t()} def one(queryable, selectors, context) def one({:error, _} = error, _selectors, _context), do: error def one(%Document{} = queryable, selectors, context) do Select.one(queryable, selectors, context) end def one(%Result{} = queryable, selectors, context) do Select.one(queryable, selectors, context) end def one(source, selectors, context) do case parse(source) do {:error, reason} -> {:error, reason} document -> Select.one(document, selectors, context) end end @doc """ Returns the accumulated result of walking the queryable, accumulating nodes that match a selector. Prefer `all` or `one`- `select` should only be used when a custom accumulator is required. Requires that a `Meeseeks.Accumulator` has been added to the context via `Meeseeks.Context.add_accumulator/2`, and will raise an error if it hasn't. Parses the source if it is not a `Meeseeks.Document` or `Meeseeks.Result`, and may return `{:error, %Meeseeks.Error{type: parser}` if there is a parse error. If multiple selections are being ran on the same unparsed source, parse first to avoid unnecessary computation. ## Examples iex> import Meeseeks.CSS iex> accumulator = %Meeseeks.Accumulator.One{} iex> context = Meeseeks.Context.add_accumulator(%{}, accumulator) iex> Meeseeks.select("1
2
3
1
}> """ @spec select(queryable, selectors, Context.t()) :: any | {:error, Error.t()} def select(queryable, selectors, context) def select({:error, _} = error, _selectors, _context), do: error def select(%Document{} = queryable, selectors, context) do Select.select(queryable, selectors, context) end def select(%Result{} = queryable, selectors, context) do Select.select(queryable, selectors, context) end def select(source, selectors, context) do case parse(source) do {:error, reason} -> {:error, reason} document -> Select.select(document, selectors, context) end end # Extract @doc """ Returns the value of an attribute in a result, or nil if there isn't one. Nil input returns `nil`. ## Examples iex> import Meeseeks.CSS iex> result = Meeseeks.one("