Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beautifulsoup: how to getting elements between other elements

html:

<h2>...</h2>
<p>...</p>
<p>...</p>
<p>...</p>
<h2>...</h2>
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>

Expected result:

<p>...</p>
<p>...</p>
<p>...</p>

I mean, getting all content between h2 elements

like image 288
juque Avatar asked Mar 22 '26 19:03

juque


1 Answers

Every element has a nextSibling method, so you can just get the first h2 element, then loop through the next siblings until you hit the second element.

Pseudo Code (assuming firstElem is the h2 element):

p_tags = []
next = firstElem.nextSilbing
while next.name != "h2":
  p_tags.append(next)
  next = next.nextSibling
like image 146
Mike Lewis Avatar answered Mar 24 '26 07:03

Mike Lewis