Beautiful soup: Insert HTML5 Tag between two paragraphs
<p>I want to automate the insertion of a html tag between two paragraphs for thousand of similar pages. The snippet is something like this (the new tag must be inserted after the paragraph of header class) :</p>

<pre><code>&lt;p align="center"&gt;&lt;span class="header"&gt;My Title&lt;/span&gt;&lt;/p&gt;
{insert new tag &lt;article&gt; here}
&lt;p align="center"&gt;bla-bla-bla&lt;/p&gt;
</code></pre>

<p>I am using Python and Beautiful soup. My difficulty is to locate the place to insert and how to insert between two paragraphs. Here is my code that so far did not work well yet. Any help?</p>

<pre><code>soup = BeautifulSoup(page, 'html.parser')
cells = soup.findAll('p', attrs={"class":"header"})
index=str(cells).index('&lt;/p&gt;&lt;p&gt;')  # search location between two paragraphs
output_line = cells[:index] + '&lt;article&gt; ' + cells[index:]
</code></pre>

<p>Wow, as the core code is showed by Trombone.I'd like to give a more complete demo.</p>

<pre><code>from bs4 import BeautifulSoup
page = """
&lt;p align="center"&gt;&lt;span class="header"&gt;My Title1&lt;/span&gt;&lt;/p&gt;
&lt;p align="center"&gt;bla-bla-bla&lt;/p&gt;
&lt;p align="center"&gt;&lt;span class="header"&gt;My Title2&lt;/span&gt;&lt;/p&gt;
&lt;p align="center"&gt;bla-bla-bla&lt;/p&gt;
&lt;p align="center"&gt;&lt;span class="header"&gt;My Title3&lt;/span&gt;&lt;/p&gt;
&lt;p align="center"&gt;bla-bla-bla&lt;/p&gt;
"""
soup = BeautifulSoup(page, "html.parser")
for header in soup.find_all('span', class_='header'):
    article = soup.new_tag('article')
    article.string = 'article content'
    header.insert_after(article)

print soup.prettify()
</code></pre>

<p>OUTPUT:</p>

<pre><code>&lt;p align="center"&gt;
 &lt;span class="header"&gt;
  My Title1
 &lt;/span&gt;
&lt;/p&gt;
&lt;article&gt;
 article content
&lt;/article&gt;
&lt;p align="center"&gt;
 bla-bla-bla
&lt;/p&gt;
&lt;p align="center"&gt;
 &lt;span class="header"&gt;
  My Title2
 &lt;/span&gt;
&lt;/p&gt;
&lt;article&gt;
 article content
&lt;/article&gt;
&lt;p align="center"&gt;
 bla-bla-bla
&lt;/p&gt;
&lt;p align="center"&gt;
 &lt;span class="header"&gt;
  My Title3
 &lt;/span&gt;
&lt;/p&gt;
&lt;article&gt;
 article content
&lt;/article&gt;
&lt;p align="center"&gt;
 bla-bla-bla
&lt;/p&gt;
</code></pre>

python html soap