title: Wrap multiple tags with BeautifulSoup
body: <p>I'm writing a python script that allow to convert a html doc into a <a href="http://lab.hakim.se/reveal-js/" rel="noreferrer">reveal.js</a> slideshow. To do this, I need to wrap multiple tags inside a <code>&lt;section&gt;</code> tag. </p>
  
  <p>It's easy to wrap a single tag inside another one using the <a href="http://www.crummy.com/software/BeautifulSoup/bs4/doc/#wrap" rel="noreferrer"><code>wrap()</code></a> method. However I can't figure out how I can wrap multiple tags.</p>
  
  <p>An example for clarification, the original html:</p>
  
  <pre><code>html_doc = """
  &lt;html&gt;
  
  &lt;head&gt;
    &lt;title&gt;The Dormouse's story&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
    &lt;h1 id="first-paragraph"&gt;First paragraph&lt;/h1&gt;
    &lt;p&gt;Some text...&lt;/p&gt;
    &lt;p&gt;Another text...&lt;/p&gt;
    &lt;div&gt;
      &lt;a href="http://link.com"&gt;Here's a link&lt;/a&gt;
    &lt;/div&gt;
  
    &lt;h1 id="second-paragraph"&gt;Second paragraph&lt;/h1&gt;
    &lt;p&gt;Some text...&lt;/p&gt;
    &lt;p&gt;Another text...&lt;/p&gt;
  
    &lt;script src="lib/.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;
  """
  
  
  """
  </code></pre>
  
  <p>I'd like to wrap the <code>&lt;h1&gt;</code> and their next tags inside <code>&lt;section&gt;</code> tags, like this:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;The Dormouse's story&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
    &lt;section&gt;
      &lt;h1 id="first-paragraph"&gt;First paragraph&lt;/h1&gt;
      &lt;p&gt;Some text...&lt;/p&gt;
      &lt;p&gt;Another text...&lt;/p&gt;
      &lt;div&gt;
        &lt;a href="http://link.com"&gt;Here's a link&lt;/a&gt;
      &lt;/div&gt;
    &lt;/section&gt;
  
    &lt;section&gt;
      &lt;h1 id="second-paragraph"&gt;Second paragraph&lt;/h1&gt;
      &lt;p&gt;Some text...&lt;/p&gt;
      &lt;p&gt;Another text...&lt;/p&gt;
    &lt;/section&gt;
  
    &lt;script src="lib/.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>Here's how I made the selection:</p>
  
  <pre><code>from bs4 import BeautifulSoup
  import itertools
  soup = BeautifulSoup(html_doc)
  h1s = soup.find_all('h1')
  for el in h1s:
      els = [i for i in itertools.takewhile(lambda x: x.name not in [el.name, 'script'], el.next_elements)]
      els.insert(0, el)
      print(els)
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>[&lt;h1 id="first-paragraph"&gt;First paragraph&lt;/h1&gt;, 'First paragraph', '\n  ', &lt;p&gt;Some text...&lt;/p&gt;, 'Some text...', '\n  ', &lt;p&gt;Another text...&lt;/p&gt;, 'Another text...', '\n  ', &lt;div&gt;&lt;a href="http://link.com"&gt;Here's a link&lt;/a&gt;  &lt;/div&gt;, '\n    ', &lt;a href="http://link.com"&gt;Here's a link&lt;/a&gt;, "Here's a link", '\n  ', '\n\n  ']
  
  [&lt;h1 id="second-paragraph"&gt;Second paragraph&lt;/h1&gt;, 'Second paragraph', '\n  ', &lt;p&gt;Some text...&lt;/p&gt;, 'Some text...', '\n  ', &lt;p&gt;Another text...&lt;/p&gt;, 'Another text...', '\n\n  ']
  </code></pre>
  
  <p>The selection is correct but I can't see how to wrap each selection inside a <code>&lt;section&gt;</code> tag.</p>