BeautifulSoup: How do I extract all the <li>s from a list of <ul>s that contains some nested <ul>s? BeautifulSoup: How do I extract all the <li>s from a list of <ul>s that contains some nested <ul>s? python python

BeautifulSoup: How do I extract all the <li>s from a list of <ul>s that contains some nested <ul>s?


.findAll() works for nested li elements:

for ul in uls:    for li in ul.findAll('li'):        print(li)

Output:

<li>List items</li><li>Etc...</li><li>List items</li><li>Nested list items</li><li>Nested list items</li><li>List items</li>


A list comprehension could work, too.

lis = [li for ul in uls for li in ul.findAll('li')]


import requestsfrom bs4 import BeautifulSoupr = requests.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_list_test")soup =   BeautifulSoup(r.content,"lxml")w3schollsList = soup.find_all('body')for w3scholl in w3schollsList:    ulList = w3scholl.find_all('li')    for li in ulList:        print(li)

Note: here is to get the "li" inside the div we made