-2

I would like to know how to extract all of the elements under a specific tag. For example:

<div class="text">
    <h2>...</h2>
    <p>...</p>
    <p>...</p>
    <h2>...</h2>
</div>

I would like to get these elements in a list

list = ['<h2>...</h2>',
        '<p>...</p>',
        '<p>...</p>',
        '<h2>...</h2>']

The reason I need this, I want to know under what category (header) the text is written and extract the text.

Nabin
  • 11,216
  • 8
  • 63
  • 98

1 Answers1

0
from bs4 import BeautifulSoup
l = soup.find('div', {'class':'text'}).findChildren()
Rahul
  • 10,830
  • 4
  • 53
  • 88