Packages
floki
0.2.1
0.38.4
0.38.3
0.38.2
0.38.1
0.38.0
0.37.1
0.37.0
0.36.3
0.36.2
0.36.1
0.36.0
0.35.4
0.35.3
0.35.2
0.35.1
0.35.0
0.34.3
0.34.2
0.34.1
0.34.0
0.33.1
0.33.0
0.32.1
0.32.0
0.31.0
0.30.1
0.30.0
0.29.0
0.28.0
0.27.0
0.26.0
0.25.0
0.24.0
0.23.1
0.23.0
0.22.0
0.21.0
0.20.4
0.20.3
0.20.2
0.20.1
0.20.0
0.19.3
0.19.2
0.19.1
0.19.0
0.18.1
0.18.0
0.17.2
0.17.1
0.17.0
0.16.0
0.15.0
0.14.0
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.10.1
0.10.0
0.9.0
0.8.1
0.8.0
0.7.2
0.7.1
0.7.0
0.6.1
0.6.0
0.5.0
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.1
0.1.0
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1
Floki is a simple HTML parser that enables search for nodes using CSS selectors.
Current section
86 Versions
Jump to
Current section
86 Versions
Compare versions
4
files changed
+42
additions
-10
deletions
| @@ -1,6 +1,11 @@ | |
| 1 | - # Floki [](https://travis-ci.org/philss/floki) [](https://hex.pm/packages/floki) [](http://inch-ci.org/github/philss/floki) |
| 1 | + # Floki |
| 2 2 | |
| 3 | - Floki is a simple HTML parser that enables search using CSS like selectors. |
| 3 | + [](https://travis-ci.org/philss/floki) |
| 4 | + [](https://hex.pm/packages/floki) |
| 5 | + [](https://hex.pm/packages/floki) |
| 6 | + [](http://inch-ci.org/github/philss/floki) |
| 7 | + |
| 8 | + Floki is a simple HTML parser that enables search using query selectors like jQuery or CSS. |
| 4 9 | |
| 5 10 | You can search elements by class, tag name and id. |
| 6 11 | |
| @@ -23,7 +28,7 @@ Assuming that you have the following HTML: | |
| 23 28 | </html> |
| 24 29 | ``` |
| 25 30 | |
| 26 | - Here are some of the queries that you can perform (with return examples): |
| 31 | + Here are some queries that you can perform (with return examples): |
| 27 32 | |
| 28 33 | ```elixir |
| 29 34 | Floki.find(html, "#content") |
| @@ -31,16 +36,20 @@ Floki.find(html, "#content") | |
| 31 36 | # => [{"p", [{"class", "headline"}], ["Floki"]}, |
| 32 37 | # => {"a", [{"href", "http://github.com/philss/floki"}], ["Github page"]}]} |
| 33 38 | |
| 39 | + |
| 34 40 | Floki.find(html, ".headline") # returns a list with the `p` element |
| 35 41 | # => [{"p", [{"class", "headline"}], ["Floki"]}] |
| 36 42 | |
| 43 | + |
| 37 44 | Floki.find(html, "a") |
| 38 45 | # => [{"a", [{"href", "http://github.com/philss/floki"}], ["Github page"]}, |
| 39 46 | # => {"a", [{"href", "https://hex.pm/packages/floki"}], ["Hex package"]}] |
| 40 47 | |
| 48 | + |
| 41 49 | Floki.find(html, "#content a") |
| 42 50 | # => [{"a", [{"href", "http://github.com/philss/floki"}], ["Github page"]}] |
| 43 51 | |
| 52 | + |
| 44 53 | Floki.find(html, ".headline, a") |
| 45 54 | # => [{"p", [{"class", "headline"}], ["Floki"]}, |
| 46 55 | # => {"a", [{"href", "http://github.com/philss/floki"}], ["Github page"]}, |
| @@ -58,17 +67,32 @@ Example of node: | |
| 58 67 | So even if the only child node is the element text, it is represented |
| 59 68 | inside a list. |
| 60 69 | |
| 61 | - You can write a simple HTML crawler (with support of [HTTPoison](https://github.com/edgurgel/httpoison)) with a few lines of code: |
| 70 | + You can write a simple HTML crawler with Floki and [HTTPoison](https://github.com/edgurgel/httpoison): |
| 62 71 | |
| 63 72 | ```elixir |
| 64 73 | html |
| 65 74 | |> Floki.find(".pages a") |
| 66 75 | |> Floki.attribute("href") |
| 67 76 | |> Enum.map(fn(url) -> HTTPoison.get!(url) end) |
| 77 | + |
| 68 78 | ``` |
| 69 79 | |
| 70 80 | It is simple as that! |
| 71 81 | |
| 82 | + ## Installation |
| 83 | + |
| 84 | + You can install Floki by adding a dependency to your mix file (mix.exs): |
| 85 | + |
| 86 | + ```elixir |
| 87 | + defp deps do |
| 88 | + [ |
| 89 | + {:floki, "~> 0.2"} |
| 90 | + ] |
| 91 | + end |
| 92 | + ``` |
| 93 | + |
| 94 | + After that, run `mix deps.get`. |
| 95 | + |
| 72 96 | ## API |
| 73 97 | |
| 74 98 | To parse a HTML document, try: |
| @@ -2,7 +2,7 @@ | |
| 2 2 | {<<"build_tools">>,[<<"mix">>]}. |
| 3 3 | {<<"contributors">>,[<<"Philip Sampaio Silva">>]}. |
| 4 4 | {<<"description">>, |
| 5 | - <<"A HTML parser and searcher.\n\nYou can search inside HTML documents using CSS like selectors.\n">>}. |
| 5 | + <<"A HTML parser and searcher.\n\nYou can search inside HTML documents using CSS like selectors.">>}. |
| 6 6 | {<<"elixir">>,<<">= 1.0.0">>}. |
| 7 7 | {<<"files">>, |
| 8 8 | [<<"lib/floki.ex">>,<<"lib/floki/deep_text.ex">>, |
| @@ -17,4 +17,4 @@ | |
| 17 17 | [{<<"app">>,<<"mochiweb">>}, |
| 18 18 | {<<"optional">>,nil}, |
| 19 19 | {<<"requirement">>,<<"~> 2.12.2">>}]}]}. |
| 20 | - {<<"version">>,<<"0.2.0">>}. |
| 20 | + {<<"version">>,<<"0.2.1">>}. |
| @@ -42,8 +42,7 @@ defmodule Floki do | |
| 42 42 | You can write a simple HTML crawler (with support of [HTTPoison](https://github.com/edgurgel/httpoison)) with a few lines of code: |
| 43 43 | |
| 44 44 | html |
| 45 | - |> Floki.find(".pages") |
| 46 | - |> Floki.find("a") |
| 45 | + |> Floki.find(".pages a") |
| 47 46 | |> Floki.attribute("href") |
| 48 47 | |> Enum.map(fn(url) -> HTTPoison.get!(url) end) |
| 49 48 | |
| @@ -60,11 +59,20 @@ defmodule Floki do | |
| 60 59 | iex> Floki.parse("<div class=js-action>hello world</div>") |
| 61 60 | {"div", [{"class", "js-action"}], ["hello world"]} |
| 62 61 | |
| 62 | + iex> Floki.parse("<div>first</div><div>second</div>") |
| 63 | + [{"div", [], ["first"]}, {"div", [], ["second"]}] |
| 64 | + |
| 63 65 | """ |
| 64 66 | |
| 67 | + @floki_root_node "floki" |
| 68 | + |
| 65 69 | @spec parse(binary) :: html_tree |
| 66 70 | |
| 67 | - def parse(html), do: :mochiweb_html.parse(html) |
| 71 | + def parse(html) do |
| 72 | + html = "<#{@floki_root_node}>#{html}</#{@floki_root_node}>" |
| 73 | + {@floki_root_node, [], parsed} = :mochiweb_html.parse(html) |
| 74 | + if length(parsed) == 1, do: hd(parsed), else: parsed |
| 75 | + end |
| 68 76 | |
| 69 77 | @doc """ |
| 70 78 | Finds elements inside a HTML tree or string. |
| @@ -3,7 +3,7 @@ defmodule Floki.Mixfile do | |
| 3 3 | |
| 4 4 | def project do |
| 5 5 | [app: :floki, |
| 6 | - version: "0.2.0", |
| 6 | + version: "0.2.1", |
| 7 7 | elixir: ">= 1.0.0", |
| 8 8 | package: package, |
| 9 9 | description: description, |