TrailingDots

TrailingDots

Floki attribute with space not selected

In attempting to parse a html table, I ran across a class that I cannot figure out how to parse. Specifically:

defmodule Embedded do
  @doc """
  Floki can parse class selectors with embedded spaces?
  """
  def embedded() do
    html = """
    <tr class="team">
       <td class="name">
         Boston Bruins
       </td>
       <td class="ot-losses">
                            
       </td>
              
        <td class="diff text-success">
           35
        </td>
    </tr>
    """

    {:ok, parsed} = Floki.parse_document(html)
    IO.puts("parsed=\n#{inspect(parsed)}")

    parsed_td = Floki.find(parsed, "td")
    IO.puts("\nparsed_td=#{inspect(parsed_td)}")

    tds = Floki.find(parsed, "td") |> Floki.attribute("class")
    IO.puts("tds=#{inspect(tds)}")
    # tds=["\nname", "ot-losses", "diff text-success"]

    name = Floki.find(parsed, "td.name") |> Floki.text()
    IO.puts("\nname=#{inspect(name)}")
    # name="\n     Boston Bruins\n   "

    ot_losses = Floki.find(parsed, "td.ot_losses") |> Floki.text()
    IO.puts("\not_losses=#{inspect(ot_losses)}")
    # name="\n     Boston Bruins\n   "

    # FAILS: html clearly shows a value of 35 
    diff = Floki.find(parsed, "td.diff text-success") |> Floki.text()
    IO.puts("\ndiff=#{inspect(diff)}")
    # diff=
  end
end

The name and ot-losses get selected ok.

What Floki function I pull out the text for the "diff text-success?
This Floki function should work all other classes in the table.

I have no control over the html - it was read from a website.
The html fragment is presented straight from the source page.

Most Liked

mindok

mindok

Hi @TrailingDots,

You aren’t selecting on a class name with a space in it, you are wanting to select on two classes. Try td.diff.text-success as your selector rather than td.diff text-success

Where Next?

Popular in Questions Top

New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

We're in Beta

About us Mission Statement