neuone
The HTML has no attributes it’s very vanilla and plain
<!doctype html>
<html>
<body>
<section class="body-copy">
<h2>Topic 1</h2>
<p>data-a</p>
<p>data-b</p>
<p>data-c</p>
<h2>Topic 2</h2>
<p>data-d</p>
<p>data-e</p>
<p>data-f</p>
<h2>Topic 3</h2>
<p>data-g</p>
<p>data-h</p>
<p>data-i</p>
</section>
</body>
</html>
I’m using Floki and I’m trying to parse it so I can create a List of maps like so.
%{ topic: "Topic 1", data: "data-a" }
%{ topic: "Topic 1", data: "data-b" }
%{ topic: "Topic 1", data: "data-c" }
%{ topic: "Topic 2", data: "data-d" }
%{ topic: "Topic 2", data: "data-e" }
%{ topic: "Topic 2", data: "data-f" }
I’m struggling to get all the P tags under each H2 with Floki.
# Try loading this html
path = "/Users/Foo/Desktop/test.html"
{_, local_file } = File.read(path)
# This will return me all the h2
Floki.find(local_file, "h2")
[{"h2", [], ["Topic 1"]}, {"h2", [], ["Topic 2"]}, {"h2", [], ["Topic 3"]}]
# This will return me the first p from a specific h2. But not all of them
Floki.find(local_file, "h2:nth-of-type(1) + p")
[{"p", [], ["data-a"]}]
# This return this first p for Topic 2 but I need 2 more p tags (data-e, data-f)
Floki.find(local_file, "h2:nth-of-type(2) + p")
[{"p", [], ["data-d"]}]
# This return this first p for Topic 3 but I need 2 more p tags (data-h, data-i)
Floki.find(local_file, "h2:nth-of-type(3) + p")
[{"p", [], ["data-g"]}]
Question
I cannot figure out how to get ONLY the P tags for each H2.
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
mischov
The problem is that the data is not actually grouped by anything other intent and order, which CSS selectors don’t do well with.
If each item looked like
<div><h2>Header</h2><p>Item 1</p><p>Item 2</p></div>then trying to select with CSS selectors would be a lot easier because that’s the type of data those selectors work best with.Here are a couple ways you could solve the problem (based on the example data):
If there are an identical number of
ptags after eachh2tag, you could get a list of all the children of the<section>and do aEnum.chunk_byto group the nodes that belong together and go from there.If there aren’t an identical number of
ptags after eachh2tag or there are other unwanted children, you could get a list of all the children of the<section>and write a reducer that builds up a map ofh2element to a list of the sections that occur before the nexth2, and go from there.There might be better solutions I’m not thinking of, but those are places where you could start.
neuone
The situation is like your number 2 point. After the
H2it just random amountptags nothing else.I will need to figure out how to write a reducer first. And then a reducer for situation number 2.
idi527
Maybe
inner_accis for collectingdata-*inptagsouter_accis for collecting%{topic => data (aka final_inner_acc)}mapscurrent_topicis for keeping the topic from the lasth2tagmischov
This is brittle as all get-out because it makes some serious assumptions about the shape of the data, but:
With your input that returns something like
neuone
This is the final result
Is this solution a good example of Recursion.
Still learning Elixir, thats why I am asking.
neuone
This result is what I wanted to achieve.
Thank you for this solution.
I don’t completely understand exactly how it works, but I’ll look into it this evening.
But I’m reading up on reducers in Elixir. I’m hoping this along with with my tutorials will get me further along.
mischov
Simply, reducers are functions that take 1) the current item from the collection being reduced over, and 2) the current accumulator, and that return a value that will be used as the accumulator in the next call of the reducer (for the next item in the collection being reduced over).
For example,
fn n, sum -> sum + n endis a reducer that take a number, and adds it to the running sum. It could be used likeIn the solution, my accumulator takes the shape of a tuple that hold the current topic (or nil) as the first element, and a list of topic maps as the second element.
The reducer has four clauses:
<p>element before it has a topic, it raises.<h2>element, it assumes that that element has one child, which will be the topic, and sets the topic in the accumulator to that topic.<p>element and has a topic, it creates a topic map with the data from the<p>element (again assuming the element only has one child) and the topic, and adds that topic map to the topic maps in the accumulator.<span>, or a<p>element with no or multiple children, or anything else), it raises.If you have any other questions about how some part of that works feel free to ask.
OvermindDL1
If you don’t need to write data ‘out’ then using Meeseeks has some far better selectors that can grab entire ranges far easier via xpath selectors and so some extended css selectors.