title: How to parse html nested blocks to lists with python BeautifulSoup?
body: <p>I am trying to convert structure like this (some nested xml/html)</p>
  
  <pre><code>&lt;div&gt;a comment
    &lt;div&gt;an answer&lt;/div&gt;
    &lt;div&gt;an answer
      &lt;div&gt;a reply&lt;/div&gt;
      ...
    &lt;/div&gt;
    ...
  &lt;/div&gt;
  ...
  </code></pre>
  
  <p><strong>clarification:</strong> it can be formatted like <code>&lt;div&gt;a comment&gt;&lt;div&gt;an answer&lt;/div&gt;</code> or in any other way (not prettified etc)</p>
  
  <p>(which has multiple nodes of different depth)</p>
  
  <p>to corresponding list structure which has parent <code>&lt;ul&gt;</code> tags (i.e. ordinary html list)</p>
  
  <pre><code>&lt;ul&gt;
    &lt;li&gt;1
      &lt;ul&gt;
        &lt;li&gt;2&lt;/li&gt;
        ...
     &lt;/ul&gt;
    &lt;/li&gt;
    ...
  &lt;/ul&gt;
  </code></pre>
  
  <p>I tried to use BeautifulSoup like this:</p>
  
  <pre><code>from bs4 import BeautifulSoup as BS
  
  bs = BS(source_xml)
  for i in bs.find_all('div'):
      i.name = 'i'
  
  # but it only replaces div tags to li tags, I still need to add ul tags
  
  I can iterate through indentation levels like this, but I still can't figure how to separate a group of tags located on the same level to add the ul tag to them:
  for i in bs.find_all('div', recursive=False):
      # how to wrap the following iterated items in 'ul' tag?
      for j in i.find_all('div', recursive=False):
           ...
  </code></pre>
  
  <p>how can one add <code>&lt;ul&gt;</code> tags in right places? (I don't care about pretty printing etc, I need valid html structure with ul and li tags, tnx...)</p>