Home > Mobile >  Loop through children of ol tag with beautifulsoup
Loop through children of ol tag with beautifulsoup

Time:06-15

I have the following html structure:

<ol>
  <li>...</li>
  <div>...</div>
  <li>...</li>
</ol>

When I do the following:

for e in doc.find('ol').children:
    print(e.text)

I get the two li elements but not the div

What can I do to get all of them in the correct order?

CodePudding user response:

Your script ist working, you could adjust it a bit and just print the text of the tags:

for e in soup.find('ol').children:
    if e.name:
        print(e.text)
Example
from bs4 import BeautifulSoup

html='''
<ol>
  <li>...li1</li>
  <div>...div</div>
  <li>...li2</li>
</ol>
'''

soup = BeautifulSoup(html, 'html.parser')

for e in soup.find('ol').children:
    if e.name:
        print(e.text)
Output
...li1
...div
...li2
  • Related