My HTML code contains nested lists like this:
<ul>
<li>Apple</li>
<li>Pear</li>
<ul>
<li>Cherry</li>
<li>Orange</li>
<ul>
<li>Pineapple</li>
</ul>
</ul>
<li>Banana</li>
</ul>
I need to parse them so they look like this:
+ Apple
+ Pear
++ Cherry
++ Orange
+++ Pineapple
+ Banana
I tried using BeautifulSoup, but I am stuck on how to consider the nesting in my code.
Example, where x contains the HTML code listed above:
import bs4
soup = bs4.BeautifulSoup(x, "html.parser")
for ul in soup.find_all("ul"):
for li in ul.find_all("li"):
li.replace_with("+ {}\n".format(li.text))