title: How to find list of all HTML Tags which are active on a particular data
body: <p>I want to parse HTML to convert it to some other format while keeping some of the styles (Bolds, lists, etc).</p>
  
  <p>To better explain what I mean,</p>
  
  <p>Consider the following code:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  
  &lt;h2&gt;A Nested List&lt;/h2&gt;
  &lt;p&gt;List &lt;b&gt;can&lt;/b&gt; be nested (lists inside lists):&lt;/p&gt;
  
  &lt;ul&gt;
    &lt;li&gt;Coffee&lt;/li&gt;
    &lt;li&gt;Tea
      &lt;ul&gt;
        &lt;li&gt;Black tea&lt;/li&gt;
        &lt;li&gt;Green tea&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;Milk&lt;/li&gt;
  &lt;/ul&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Now if I were to select the word "List" at the start of the paragraph, my output should be (html, body,p), since those are the tags active on the word "List".</p>
  
  <p>Another example, if I were to select the word "Black tea", my output should be (html,body,ul,li,ul,li), since it's part of the nested list.</p>
  
  <p>I've seen chrome inspector do this but I'm not sure how I can do this in code by using Python.</p>
  
  <p>Here is an Image of what the chrome inspector shows: 
  <a href="https://i.imgur.com/Dbz9F2W.png" rel="nofollow noreferrer">Chrome Inspector</a></p>
  
  <p>I've tried parsing through the HTML using Beautiful soup and while it is amazing for getting a data, I was unable to solve my problem using it.</p>
  
  <p>Later I tried the html-parser for this same issue, trying to make a stack of all tags before a "data" and popping them out as I encounter corresponding end-tags, but I couldn't do it either.</p>