# Counting the number of paragraphs ("p") between containers ("div")

**URL:** <https://discourse.julialang.org/t/counting-the-number-of-paragraphs-p-between-containers-div/92576>\
**Category:** General Usage\
**Tags:** web, website, http\
**Created:** [January 5, 2023, 9:49pm UTC](https://discourse.julialang.org/t/counting-the-number-of-paragraphs-p-between-containers-div/92576 "2023-01-05T21:49:37Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [January 6, 2023, 1:03pm UTC](https://discourse.julialang.org/t/counting-the-number-of-paragraphs-p-between-containers-div/92576/21 "2023-01-06T13:03:34Z")

</div>

Just to be perfectly clear. If the structure is like this

```julia
**subhead**

subrow
  p
  p
  p

**subhead**

subrow
  p
  p
subrow
  p
subrow
  p

**subhead**

subrow
  p
subrow
  p

```

the desired output is `[3,4,2]`? Indentations means “child of”.

Maybe

```julia
function countp(body::HTMLNode)
   i = 0
   counts = Int[]
   for e in body.children
     if e isa HTMLElement{:div} && get(e.attributes, "class", "")=="ttt-subhead"
       push!(counts, 0)
       i += 1
     end
     counts[i] += length(eachmatch(Selector("div.ttt-row > p"), e))
   end
   counts
end

```

---

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [January 6, 2023, 1:35pm UTC](https://discourse.julialang.org/t/counting-the-number-of-paragraphs-p-between-containers-div/92576/22 "2023-01-06T13:35:11Z")

</div>

I appreciate your help, but the code output is empty.

Could it be because there is a misunderstanding. The “ttt-subhead” are essentially “islands”, and so are the clusters of “div.ttt-row \> p”.

 ![HTMLScreenshot](https://global.discourse-cdn.com/julialang/original/3X/5/8/583393a996fac8a4af99d2a4713c563a7011cadd.jpeg)

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [January 6, 2023, 1:47pm UTC](https://discourse.julialang.org/t/counting-the-number-of-paragraphs-p-between-containers-div/92576/23 "2023-01-06T13:47:11Z")

</div>

Maybe because you passed in the root node, not the body? My bad. Poor naming.

> **HTML**
>
> ```julia
> myhtml = """
> <!DOCTYPE html>
> <html lang="en">
> 
> </head>
> 
> <body style="text-align: center;">
> 
> <div class="ttt-subhead"><span>8:00</span> Breakfast</div>
> 
> <div class="ttt-row">
> <p>
> Eggs.
> </p>
> <p>
> Bacon.
> </p>
> </div>
> 
> <div class="ttt-subhead"><span>12:00</span> Lunch</div>
> 
> <div class="ttt-row">
> <p>
> Burger.
> </p>
> <p>
> Fries.
> </p>
> <p>
> Coke.
> </p>
> </div>
> <div class="ttt-row">
> <p>
> Burger.
> </p>
> <p>
> Fries.
> </p>
> <p>
> Coke.
> </p>
> </div>
> 
> <div class="ttt-subhead"><span>18:00</span> Dinner</div>
> 
> <div class="ttt-row">
> <p>
> Salad.
> </p>
> </div>
> 
> </body>
> 
> </html>
> """
> 
> ```

```julia
dom = parsehtml(myhtml)
body = dom.root[2]

countp(body)
3-element Vector{Int64}:
 2
 6
 1
```

[Previous page](https://discourse.julialang.org/t/counting-the-number-of-paragraphs-p-between-containers-div/92576.md?page=1)
