title: Creating a JSON structure out of HTML with BeautifulSoup
body: <p>I am working with BeautifulSoup, trying to parse a simple HTML document that looks like this: </p>
  
  <p><img src="https://i.stack.imgur.com/AIKXX.jpg" alt="enter image description here"></p>
  
  <p>(Naturally, the structure continues on this basic manner a few levels deeper.  However, occasionally there is an extra bullet, which provides no information, thus I would like to ignore it.)</p>
  
  <p>My goal is to parse this into JSON format.  </p>
  
  <p>I would like the end result look like:  </p>
  
  <pre><code>{
      "Outer List": {
          "Inner List" : [
              "info 1", 
              "info 2", 
              "info 3"
          ]
      }
  }
  </code></pre>
  
  <p>The code below (all thanks due @Zero Piraeus) comes very close to working:
  <a href="https://stackoverflow.com/questions/17850121/parsing-nested-html-list-with-beautifulsoup">Parsing nested HTML list with BeautifulSoup</a> </p>
  
  <pre><code>from bs4 import BeautifulSoup
  from pprint import pprint
  soup = BeautifulSoup("""
     &lt;html&gt;
       &lt;body&gt;
        &lt;ul class="rootList"&gt;
         &lt;li class="liItem endPlus"&gt;
          &lt;span class="itemToBeAdded"&gt;
           Outer List
          &lt;/span&gt;
         &lt;/li&gt;
         &lt;li class="noBulletsLi "&gt;
          &lt;ul class="innerUl"&gt;
           &lt;li class="liItem crossPlus"&gt;
            &lt;span class="itemToBeAdded"&gt;
             Inner List
            &lt;/span&gt;
            &lt;ul class="grayStarUl "&gt;
             &lt;li class=""&gt;
              &lt;span class="phrasesToBeAdded"&gt;
               info 1
              &lt;/span&gt;
             &lt;/li&gt;
             &lt;li class=""&gt;
              &lt;span class="phrasesToBeAdded"&gt;
               info 2
              &lt;/span&gt;
             &lt;/li&gt;
             &lt;li class=""&gt;
              &lt;span class="phrasesToBeAdded"&gt;
               info 3
              &lt;/span&gt;
             &lt;/li&gt;
            &lt;/ul&gt;
           &lt;/li&gt;
            &lt;/ul&gt;
           &lt;/li&gt;
          &lt;/ul&gt;
       &lt;/body&gt;
      &lt;/html&gt;
  """)
  
  ul = soup.body.ul
  
  def dictify(ul):
      result = {}
      for li in ul.find_all("li", recursive=False):
          list = []
          key = next(li.stripped_strings)
          ul = li.find("ul")
          if ul:
              result[key] = dictify(ul)
          else:
              result[key] = None
      return result
  </code></pre>
  
  <p>This bit of code returns:</p>
  
  <pre><code>{u'Inner List': {u'Inner List': {u'info 1': None,
                                   u'info 2': None,
                                   u'info 3': None}},
   u'Outer List': None}
  </code></pre>
  
  <p>Whereas I'm trying to reach:</p>
  
  <pre><code>{u'Outer List': {u'Inner List': [u'info 1',
                                    'info 2',
                                    'info 3']}
  </code></pre>
  
  <p>How can I get my <code>'info X'</code> items into a list as the value, in a hopefully reasonably scalable manner....and ignore those pesky bullets that may be present?</p>
  
  <p>Thank you for reading this far!</p>