Floki parses HTML and finds nodes using CSS selectors. Elixir code can parse documents or fragments, query trees, read attributes and text, and serialize nodes back to HTML.
Overview
Floki is an Elixir library for parsing HTML and locating elements with CSS selectors. Parsed documents and fragments become trees of tuples tagged with element names, attribute lists, and child nodes, including text nodes wrapped in lists as documented in the API.
Typical usage parses a document or fragment, runs selector queries such as class or ID filters, extracts attributes or inner text, and can render a node tree back to HTML. The selector support spans element types, attribute matchers, structural combinators, positional pseudo-classes, state pseudo-classes like checked and disabled, :has and :not with simple-selector limits, and Floki-specific text containment selectors.
By default Floki relies on a patched mochiweb_html parser bundled for easy installation. Optional backends include html5ever via Rustler precompiled NIFs and fasthtml built on lexbor when those dependencies are configured in the application.
The library integrates into Mix projects as a dependency and can be loaded in scripts through Mix.install. It may emit debug or info logs around selector or tree parsing unless logger compile-time purge options are set in application configuration.
Key features
- CSS selector queries
- Document and fragment parsing
- Attribute and text extraction
- HTML serialization from nodes
- Optional html5ever backend
- Optional fasthtml backend
- Floki-specific text selectors
- Mix and Livebook installation
Use cases
- Scraping and HTML extraction in Elixir
- Testing and parsing web snippets
- Feed and markup transformation pipelines
- Selector-based DOM traversal
- Fragment cleanup and rewriting