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
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Hello
I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
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
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #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.