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
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











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.