Floki

A HTML parser and seeker.

This is a simple HTML parser that enables searching using CSS like selectors.

You can search elements by class, tag name and id.

Example

Assuming that you have the following HTML:

<!doctype html>
<html>
<body>
  <section id="content">
    <p class="headline">Floki</p>
    <a href="http://github.com/philss/floki">Github page</a>
  </section>
</body>
</html>

You can perform the following queries:

Each HTML node is represented by a tuple like:

{tag_name, attributes, chidren_nodes}

Example of node:

{"p", [{"class", "headline"}], ["Floki"]}

So even if the only child node is the element text, it is represented inside a list.

You can write a simple HTML crawler (with support of HTTPoison) with a few lines of code:

html
|> Floki.find(".pages")
|> Floki.find("a")
|> Floki.attribute("href")
|> Enum.map(fn(url) -> HTTPoison.get!(url) end)

It is simple as that!

Summary

attribute(elements, attribute_name)

Returns a list with attribute values from elements

attribute(html, selector, attribute_name)

Returns a list with attribute values for a given selector

find(html, selector)

Finds elements inside a HTML tree or string. You can search by class, tag name or id

parse(html)

Parses a HTML string

text(html)

Returns the text nodes from a html tree

Types

html_tree :: tuple | list

Functions

attribute(elements, attribute_name)

Specs:

  • attribute(binary | html_tree, binary) :: list

Returns a list with attribute values from elements.

Examples

iex> Floki.attribute("<a href='https://google.com'>Google</a>", "href")
["https://google.com"]
attribute(html, selector, attribute_name)

Specs:

  • attribute(binary | html_tree, binary, binary) :: list

Returns a list with attribute values for a given selector.

Examples

iex> Floki.attribute("<a href='https://google.com'>Google</a>", "a", "href")
["https://google.com"]
find(html, selector)

Specs:

Finds elements inside a HTML tree or string. You can search by class, tag name or id.

It is possible to compose searches:

Floki.find(html_string, ".class")
|> Floki.find(".another-class-inside-small-scope")

Examples

iex> Floki.find("<p><span class=hint>hello</span></p>", ".hint")
[{"span", [{"class", "hint"}], ["hello"]}]

iex> "<body><div id=important><div>Content</div></div></body>" |> Floki.find("#important")
{"div", [{"id", "important"}], [{"div", [], ["Content"]}]}

iex> Floki.find("<p><a href='https://google.com'>Google</a></p>", "a")
[{"a", [{"href", "https://google.com"}], ["Google"]}]
parse(html)

Specs:

Parses a HTML string.

Examples

iex> Floki.parse("<div class=js-action>hello world</div>")
{"div", [{"class", "js-action"}], ["hello world"]}
text(html)

Specs:

Returns the text nodes from a html tree.

Examples

iex> Floki.text("<div><span>something else</span>hello world</div>")
"hello world"