How do I use Beautiful Soup to get everything between two tags?

<h4>1</h4>

text

text

text

<h4>2</h4>

text

text

text

<span>asdf</span> <h4>3</h4>
4
1
1
2
2

text

description 4

text

1
2

HTML code as above, how do I get the content between two

? For example,

  • get everything between description 1 and description 2 for the first time
  • get everything between "description 2" and "description 3" for the second time


r = requests.get (url,headers=headers)
html = r.text
soup = BeautifulSoup (, 'html.parser')
result1 = soup.find_all (' h4') [0:1]
result2 = soup.find_all ('h4') [1:2]


assume that all h4 tags are siblings. You can call next_siblings to enumerate all its sibling elements, such as

.
-sharp -*- coding: utf-8 -*-
from bs4 import BeautifulSoup

html_doc = '''
<html>
    <body>
        <h4>1</h4>
        

p1.1

p1.2

<h4>2</h4>

p2.1

p2.2

<span>span2.1</span> </body> </html> ''' soup = BeautifulSoup(html_doc, 'html.parser') all_h4 = soup.select('h4') for h4 in all_h4: print('====') print(f'{h4!r}') for sibling in h4.next_siblings: if sibling in all_h4: break print(f' {sibling!r}')

output results

====
<h4>1</h4>
  '\n'
  

p1.1

'\n'

p1.2

'\n' ==== <h4>2</h4> '\n'

p2.1

'\n'

p2.2

'\n' <span>span2.1</span> '\n'
Menu