Meeseeks is an Elixir library for parsing and extracting data from HTML and XML with CSS or XPath selectors.
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: "..."}, ...]
See the HexDocs for additional documentation.
- Friendly API
- Browser-grade HTML5 parser
- Permissive XML parser
- CSS and XPath selectors
- Rich, extensible selector architecture
- Helpers to extract data from selections
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?
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 via Rustler after Hans 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.
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<{ <p>1</p> }>
) and the descriptive errors.
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 Floki provides facilities to do so while Meeseeks, which is entirely focused on selecting and extracting data, does not.
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.
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.
Meeseeks depends on html5ever via meeseeks_html5ever.
Because html5ever is a Rust library, you will need to have the Rust compiler installed.
This dependency is necessary because there are no HTML5 spec compliant parsers written in Elixir/Erlang.
Most Heroku buildpacks for Elixir do not come with Rust installed; you will need to:
- Add a Rust buildpack to your app, setting it to run before Elixir; and
- Add a
RustConfig
file to your project's root directory, withRUST_SKIP_BUILD=1
set.
For example:
heroku buildpacks:add -i 1 https://github.com/emk/heroku-buildpack-rust.git
echo "RUST_SKIP_BUILD=1" > RustConfig
Ensure Rust is installed, then add Meeseeks to your mix.exs
:
defp deps do
[
{:meeseeks, "~> 0.11.0"}
]
end
Finally, run mix deps.get
.
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.
document = Meeseeks.parse("<div id=main><p>1</p><p>2</p><p>3</p></div>")
#=> #Meeseeks.Document<{...}>
The selection functions accept an unparsed source, parsing it as HTML, but parsing is expensive so parse ahead of time when running multiple selections on the same document.
Next, use one of Meeseeks's selection functions - fetch_all
, all
, fetch_one
, or one
- to search for nodes.
All these functions accept a queryable (a source, a document, or a Meeseeks.Result
), one or more Meeseeks.Selector
s, and optionally an initial context.
all
returns a (possibly empty) list of results representing every node matching one of the provided selectors, while one
returns a result representing the first node to match a selector (depth-first) or nil if there is no match.
fetch_all
and fetch_one
work like all
and one
respectively, but wrap the result in {:ok, ...}
if there is a match or return {:error, %Meeseeks.Error{type: :select, reason: :no_match}}
if there is not.
To generate selectors, use the css
macro provided by Meeseeks.CSS
or the xpath
macro provided by Meeseeks.XPath
.
import Meeseeks.CSS
result = Meeseeks.one(document, css("#main p"))
#=> #Meeseeks.Result<{ <p>1</p> }>
import Meeseeks.XPath
result = Meeseeks.one(document, xpath("//*[@id='main']//p"))
#=> #Meeseeks.Result<{ <p>1</p> }>
Retrieve information from the Meeseeks.Result
with an extraction function.
The extraction functions are attr
, attrs
, data
, dataset
, html
, own_text
, tag
, text
, tree
.
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.
Meeseeks.html(document)
#=> "<html><head></head><body><div id=\"main\"><p>1</p><p>2</p><p>3</p></div></body></html>"
Meeseeks is designed to have extremely extensible selectors, and creating a custom selector is as easy as defining a struct that implements the Meeseeks.Selector
behaviour.
defmodule CommentContainsSelector do
use Meeseeks.Selector
alias Meeseeks.Document
defstruct value: ""
def match(selector, %Document.Comment{} = node, _document, _context) do
String.contains?(node.content, selector.value)
end
def match(_selector, _node, _document, _context) do
false
end
end
selector = %CommentContainsSelector{value: "TODO"}
Meeseeks.one("<!-- TODO: Close vuln! -->", selector)
#=> #Meeseeks.Result<{ <!-- TODO: Close vuln! --> }>
To learn more, check the documentation for Meeseeks.Selector
and Meeseeks.Selector.Combinator
Contributions are very welcome, especially bug reports.
If submitting a bug report, please search open and closed issues first.
To make a pull request, fork the project, create a topic branch off of master
, push your topic branch to your fork, and open a pull request.
If you're submitting a bug fix, please include a test or tests that would have caught the problem.
If you're submitting new features, please test and document as appropriate.
Before submitting a PR, please run mix format
.
By submitting a patch, you agree to license your work under the license of this project.
$ git clone https://github.com/mischov/meeseeks.git
$ cd meeseeks
$ mix deps.get
$ mix test
$ MIX_ENV=docs mix docs
Meeseeks is licensed under the MIT License