How to remove HTML tags in BeautifulSoup when I have contents
<p>The html for what I'm attempting to grab:</p>

<p><code>&lt;div id="unitType"&gt;
     &lt;h2&gt;BB100 &lt;br&gt;v1.4.3&lt;/h2&gt;
 &lt;/div&gt;</code></p>

<p>I have the contents of an <code>h2</code> tag below:</p>

<pre><code>initialPage = beautifulSoup(urllib.urlopen(url).read(), 'html.parser')
deviceInfo = initialPage.find('div', {'id': 'unitType'}).h2.contents
print('Device Info: ', deviceInfo)
for i in deviceInfo:
    print i
</code></pre>

<p>Which outputs:</p>

<pre><code>('Device Info: ', [u'BB100 ', &lt;br&gt;v1.4.3&lt;/br&gt;])
BB100
&lt;br&gt;v1.4.3&lt;/br&gt;
</code></pre>

<p>How do I remove the <code>&lt;h2&gt;</code>,<code>&lt;/h2&gt;</code>,<code>&lt;br&gt;</code> and <code>&lt;/br&gt;</code> html tags, using BeautifulSoup rather than regex? I've tried <code>i.decompose()</code> and <code>i.strip()</code> but neither has worked. It would throw <code>'NoneType' object is not callable</code>.</p>

<p>You can check if the element is a <code>&lt;br&gt;</code> tag with <code>if i.name == 'br'</code>, and then just change the list to have the contents instead.</p>

<pre><code>for i in deviceInfo:
    if i.name == 'br':
        i = i.contents
</code></pre>

<p>If you need to iterate over it many times, modify the list.</p>

<pre><code>for n, i in enumerate(deviceInfo):
    if i.name == 'br':
        i = i.contents
        deviceInfo[n] = i
</code></pre>

python python-2.7 beautifulsoup