View Source Pages.Html (Pages v0.2.0)
Some simple HTML query functions, originally intended for unit tests. Delegates the hard work to Floki.
The main query functions are:
all
: returns all elements matching the selectorfirst
: returns the first element that matches the selectorfirst!
: likefirst
but raises if more than one element matches the selector
Selectors can be a valid CSS selector string, or can be a keyword list. See Pages.Css
for keyword list syntax.
The attr/2
function can be used to extract attr values, and the text/1
function can be used to extract
the text of an HTML fragment.
usage
Usage
Example HTML:
<div class="profile-list" id="profiles">
<div class="profile admin" id="alice">
<div class="name">Alice</div>
</div>
<div class="profile" id="billy">
<div class="name">Billy</div>
</div>
</div>
Get Alice's name:
iex> Pages.Html.find!(html, "#alice .name") |> Pages.Html.text()
"Alice"
Get the names of all the profiles:
iex> Pages.Html.all(html, ".profile .name") |> Enum.map(&Pages.Html.text/1)
["Alice", "Billy"]
Link to this section Summary
Link to this section Types
Link to this section Functions
@spec all(html(), selector()) :: Floki.html_tree()
Finds all elements in html
that match selector
.
Returns the value of an attr from the outermost element of an HTML node
@spec find!(html(), selector()) :: Floki.html_node()
Like find/2
but raises unless exactly one element is found
@spec find(html(), selector()) :: Floki.html_node()
Finds the first element in html
that matches selector
@spec parse(html()) :: Floki.html_tree()
@spec parse_doc(binary()) :: Floki.html_tree()
Returns the text value of html