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 in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 8 of 8 Posts
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.