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>Just use find and <em>extract</em> the <em>br</em> tag:</p>

<pre><code>In [15]: from bs4 import BeautifulSoup
    ...: 
    ...: h = """&lt;div id='unitType'&gt;&lt;h2&gt;BB10&lt;br&gt;v1.4.3&lt;/h2&gt;&lt;/d
    ...: iv&gt;"""
    ...: 
    ...: soup = BeautifulSoup(h, "html.parser")
    ...: 
    ...: h2 = soup.find(id="unitType").h2
    ...: h2.find("br").extract()
    ...: print(h2)
    ...: 
&lt;h2&gt;BB10&lt;/h2&gt;
</code></pre>

<p>Or to replace the tag with just the text using <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#replace-with" rel="nofollow">replace-with</a>:</p>

<pre><code>In [16]: from bs4 import BeautifulSoup
    ...: 
    ...: h = """&lt;div id='unitType'&gt;&lt;h2&lt;br&gt;v1.4.3 BB10&lt;/h2&gt;&lt;/d
    ...: iv&gt;"""
    ...: 
    ...: soup = BeautifulSoup(h, "html.parser")
    ...: 
    ...: h2 = soup.find(id="unitType").h2
    ...: 
    ...: br = h2.find("br")
    ...: br.replace_with(br.text)
    ...: print(h2)
    ...: 
&lt;h2&gt;v1.4.3 BB10&lt;/h2&gt;
</code></pre>

<p>To remove the <em>h2</em> and keep the text:</p>

<pre><code>In [37]: h = """&lt;div id='unitType'&gt;&lt;h2&gt;&lt;br&gt;v1.4.3&lt;/h2&gt;&lt;/d
    ...: 
    ...: iv&gt;"""
    ...: 
    ...: soup = BeautifulSoup(h, "html.parser")
    ...: 
    ...: unit = soup.find(id="unitType")
    ...: 
    ...: h2 = unit.find("h2")
    ...: h2.replace_with(h2.text)
    ...: print(unit)
    ...: 
&lt;div id="unitType"&gt;v1.4.3 BB10&lt;/div&gt;
</code></pre>

<p>If you just want <code>"v1.4.3"</code> and  <code>"BB10"</code>, there are many ways to hey them:</p>

<pre><code>In [60]: h = """&lt;div id="unitType"&gt;
    ...:      &lt;h2&gt;BB100 &lt;br&gt;v1.4.3&lt;/h2&gt;
    ...:  &lt;/div&gt;"""
    ...: 
    ...: soup = BeautifulSoup(h, "html.parser")
    ...: 
    ...: h2 = soup.find(id="unitType").h2
        # just find all strings
    ...: a,b = h2.find_all(text=True)
    ...: print(a, b)
         # get the br
    ...: br = h2.find("br")
        # get br text and just the h2 text ignoring any text from children
    ...: a, b = h2.find(text=True, recursive=False),  br.text
    ...: print(a, b)
    ...: 
BB100  v1.4.3
BB100  v1.4.3
</code></pre>

<p>Why you  end up with text ins</p>

python python-2.7 beautifulsoup