Floki
Github page philss### Extracting Attributes with Floki.attribute/2 - Elixir Source: https://context7_llms These examples demonstrate Floki.attribute/2, which extracts a list of attribute values from elements. It takes the HTML structure and the attribute name, returning a list of corresponding values. ```Elixir iex> Floki.attribute([{"a", [{"href", "https://google.com"}], ["Google"]}], "href") ["https://google.com"] iex> Floki.attribute([{"a", [{"href", "https://google.com"}, {"data-name", "google"}], ["Google"]}], "data-name") ["google"] ``` -------------------------------- ### Querying HTML Nodes with Floki.find/2 - Elixir Source: https://context7_llms These examples illustrate various ways to query the parsed HTML document using Floki.find/2 with different CSS selectors, including ID, class, tag name, attribute selectors, and combined selectors. ```Elixir Floki.find(html, "#content") Floki.find(html, ".headline") Floki.find(html, "a") Floki.find(html, "[data-model=user]") Floki.find(html, "#content a") Floki.find(html, ".headline, a") ``` -------------------------------- ### Parsing HTML Document with Floki.parse_document! (Elixir) Source: https://context7_llms This example demonstrates `Floki.parse_document!/2`, a variant of `Floki.parse_document/1` that parses an HTML string. Unlike its counterpart, this function raises a `Floki.ParseError` if any error occurs during the parsing process, making it suitable for scenarios where immediate error feedback is desired. ```Elixir Floki.parse_document!(" hello ") ``` -------------------------------- ### Finding Elements with Floki.find/2 (from fragment) - Elixir Source: https://context7_llms These examples demonstrate Floki.find/2 for locating elements within an HTML tree or string, often after parsing a fragment. It shows how to find elements using various CSS selectors like class, ID, and tag name, and also nested selectors. ```Elixir iex> {:ok, html} = Floki.parse_fragment(" hello ") iex> Floki.find(html, ".hint") [{"span", [{"class", "hint"}], ["hello"]}] iex> {:ok, html} = Floki.parse_fragment(" Content ") iex> Floki.find(html, "#important") [{"div", [{"id", "important"}], [{"div", [], ["Content"]}]}] iex> {:ok, html} = Floki.parse_fragment(" Google ") iex> Floki.find(html, "a") [{"a", [{"href", "https://google.com"}], ["Google"]}] iex> Floki.find([{ "div", [], [{"a", [{"href", "https://google.com"}], ["Google"]}]}], "div a") [{"a", [{"href", "https://google.com"}], ["Google"]}] ``` -------------------------------- ### Extracting Attributes with Floki.attribute/3 (with selector) - Elixir Source: https://context7_llms These examples illustrate Floki.attribute/3, which extracts attribute values for elements matching a given CSS selector. It takes the HTML structure, selector, and attribute name, returning a list of values. ```Elixir iex> Floki.attribute([{"a", [{"href", "https://google.com"}], ["Google"]}], "a", "href") ["https://google.com"] iex> Floki.attribute( [{"a", [{"class", "foo"}, {"href", "https://google.com"}], ["Google"]}], "a", "class" ) ["foo"] iex> Floki.attribute( [{"a", [{"href", "https://e.corp.com"}, {"data-name", "e.corp"}], ["E.Corp"]}], "a[data-name]", "data-name" ) ["e.corp"] ``` -------------------------------- ### Escaping Strings for CSS Identifiers with Floki.css_escape/1 - Elixir Source: https://context7_llms These examples demonstrate Floki.css_escape/1, which escapes a given string to make it safe for use as a CSS identifier. This is crucial for handling strings with special characters in CSS selectors. ```Elixir iex> Floki.css_escape("hello world") "hello\\ world" iex> Floki.css_escape("-123") "-\\31 23" ``` -------------------------------- ### Finding HTML Element by ID with Floki.get_by_id (Elixir) Source: https://context7_llms This example illustrates `Floki.get_by_id/2`, which finds the first HTML element by its ID within a given HTML tree. It's particularly useful for IDs containing special characters that might be invalid in standard CSS selectors. The function returns the element if found, otherwise `nil`. ```Elixir {:ok, html} = Floki.parse_fragment(~s[ hello ]) Floki.get_by_id(html, "id?foo_special:chars") Floki.get_by_id(html, "does-not-exist") ``` -------------------------------- ### Changing Attributes with Floki.attr/4 - Elixir Source: https://context7_llms These examples show how to use Floki.attr/4 to modify attribute values of elements matched by a CSS selector. It takes the HTML structure, selector, attribute name, and a mutation function, returning the updated element tree. ```Elixir iex> Floki.attr([{"div", [{"id", "a"}], []}], "#a", "id", fn(id) -> String.replace(id, "a", "b") end) [{"div", [{"id", "b"}], []}] iex> Floki.attr([{"div", [{"class", "name"}], []}], "div", "id", fn _ -> "b" end) [{"div", [{"id", "b"}, {"class", "name"}], []}] ``` -------------------------------- ### Filtering Out Nodes with Floki.filter_out/2 - Elixir Source: https://context7_llms These examples illustrate Floki.filter_out/2, which removes nodes from an HTML tree that match a specified filter selector. It can filter by tag name, and even by special types like :comment or :text. ```Elixir iex> Floki.filter_out({"div", [], [{"script", [], ["hello"]}, " world"]}, "script") {"div", [], [" world"]} iex> Floki.filter_out([{"body", [], [{"script", [], []}, {"div", [], []}]}], "script") [{"body", [], [{"div", [], []}]}] iex> Floki.filter_out({"div", [], [{:comment, "comment"}, " text"]}, :comment) {"div", [], [" text"]} iex> Floki.filter_out({"div", [], ["text"]}, :text) {"div", [], []} ``` -------------------------------- ### Accessing Child Nodes with Floki.children/2 - Elixir Source: https://context7_llms These examples show how Floki.children/2 retrieves the direct child nodes of an HTML node. It can optionally exclude text nodes using the include_text: false option and returns nil if the node is not an HTML tag. ```Elixir iex> Floki.children({"div", [], ["text", {"span", [], []}]}) ["text", {"span", [], []}] iex> Floki.children({"div", [], ["text", {"span", [], []}]}, include_text: false) [{"span", [], []}] iex> Floki.children({:comment, "comment"}) nil ``` -------------------------------- ### Deleting Span Tags and Counting with Floki.traverse_and_update/3 (Elixir) Source: https://context7_llms This example illustrates using Floki.traverse_and_update/3 to remove specific HTML tags (e.g., 'span' tags) from a tree. It also uses an accumulator to count the number of deleted nodes, returning 'nil' for the node to be deleted. ```Elixir html = {"div", [], [{"span", [], ["hello"]}]} Floki.traverse_and_update(html, [deleted: 0], fn {"span", _attrs, _children}, acc -> {nil, Keyword.put(acc, :deleted, acc[:deleted] + 1)} tag, acc -> {tag, acc} end) ``` -------------------------------- ### Configuring Floki HTML Parser (Elixir) Source: https://context7_llms This snippet shows two ways to configure the HTML parser in Floki. The first line demonstrates setting the parser for a single function call using the ':html_parser' option, while the second block shows how to set a global default parser for the ':floki' application using Mix.Config. ```Elixir Floki.parse_document(document, html_parser: Floki.HTMLParser.FastHtml) # Or: use Mix.Config config :floki, :html_parser, Floki.HTMLParser.Mochiweb ``` -------------------------------- ### Sample HTML Document for Floki Parsing Source: https://context7_llms This HTML snippet represents a typical document structure that can be parsed and queried using the Floki library. It includes various elements with IDs, classes, and data attributes, demonstrating common HTML features. ```HTML
Floki
Github page philss