title: How to insert tag before a string in html using python
body: <p>I am finding all the text in the html document and I want to add span tag that holds some information about each text like below</p>
  
  <pre><code>def recursiveChildren(x):
      if "childGenerator" in dir(x):
        for child in x.childGenerator():
  
            recursiveChildren(child)
      else:
  
        if not x.isspace():
  
            content = x.string
            new_tag = soup.new_tag("span")
            content.insert_before(new_tag)
  if __name__ == "__main__":
  
  with open("path to html",'r') as file:
        soup = BeautifulSoup(file)
  for child in soup.body.childGenerator():
     recursiveChildren(child)
  </code></pre>
  
  <p>I am getting error as string object has no attribute insert before. How can I insert tag before string. I also tried getting the parent tag of the string and add add span tag before parent tag contents but it creates a problem if there are nested tags as I want a span tag before each and every independent string. Is it Possible? something like below but in Python <a href="https://stackoverflow.com/questions/37270726/insert-span-tag-before-content-text">Insert span tag before # content text</a></p>
  
  <p><a href="https://stackoverflow.com/questions/14861916/adding-span-before-specific-string-with-jquery">Adding Span before specific string with jQuery</a></p>
  
  <p>Example Html:</p>
  
  <pre><code>&lt;p class=MsoNormal &gt;This is first string &lt;b&gt; Second String &lt;/b&gt; , &lt;/span&gt;&lt;span style='font-family:"Times New Roman"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
  </code></pre>
  
  <p>I want the output like</p>
  
  <pre><code>&lt;p class=MsoNormal &gt;**&lt;span&gt;**This is first string&lt;/span&gt; &lt;b&gt;**&lt;span&gt;** Second String &lt;/span&gt;&lt;/b&gt;**&lt;span&gt;** , &lt;/span&gt;&lt;span style='font-family:"Times New Roman"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
  </code></pre>