An Elixir library for parsing and extracting data from HTML and XML with CSS or XPath selectors.
Branch: master
Clone or download
Latest commit 091e167 Sep 27, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config Init at 0.1.0 Feb 24, 2017
lib
src Fix CSS selector tokenization bug Sep 18, 2017
test Fix error in Select.handle_match Jun 23, 2018
.formatter.exs
.gitignore
.travis.yml
CHANGELOG.md
LICENSE Init at 0.1.0 Feb 24, 2017
LICENSE-APACHE Add XPath selectors May 13, 2017
README.md Release v0.10.1 Sep 28, 2018
mix.exs Release v0.10.1 Sep 28, 2018
mix.lock Update to meeseeks_html5ever v0.10.1 Sep 28, 2018

README.md

Meeseeks

Build Status Meeseeks version

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 HexDocs for additional documentation.

Dependencies

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.

Deploying to Heroku?

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, with RUST_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

Installation

Ensure Rust is installed, then add Meeseeks to your mix.exs:

defp deps do
  [
    {:meeseeks, "~> 0.10.1"}
  ]
end

Finally, run mix deps.get.

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.

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.

Select

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.Selectors, 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> }>

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.

Meeseeks.tag(result)
#=> "p"
Meeseeks.text(result)
#=> "1"
Meeseeks.tree(result)
#=> {"p", [], ["1"]}

The extraction functions html and tree work on Meeseeks.Documents in addition to Meeseeks.Results.

Meeseeks.html(document)
#=> "<html><head></head><body><div id=\"main\"><p>1</p><p>2</p><p>3</p></div></body></html>"

Custom Selectors

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

Contribute

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.

Running Tests

$ git clone https://github.com/mischov/meeseeks.git
$ cd meeseeks
$ mix deps.get
$ mix test

Building Docs

$ MIX_ENV=docs mix docs

License

Meeseeks is licensed under the MIT License