Extracting text within tag with BeautifulSoup
<pre><code>    &lt;div&gt;
&lt;p class="tabbed" style="margin-top:2px;"&gt;&lt;span class="tab"&gt;&lt;strong&gt;LANGUAGES&lt;/strong&gt;&lt;/span&gt;Cantonese&lt;/p&gt;
&lt;p class="tabbed" style="margin-top:2px;"&gt;&lt;span class="tab"&gt;&lt;/span&gt;English&lt;/p&gt;
&lt;p class="tabbed" style="margin-top:2px;"&gt;&lt;span class="tab"&gt;&lt;/span&gt;Putonghua&lt;/p&gt;
&lt;p class="tabbed"&gt;&lt;span class="tab"&gt;&lt;strong&gt;GENDER&lt;/strong&gt;&lt;/span&gt;Male&lt;/p&gt;
   &lt;/div&gt;
</code></pre>

<p>I would like to extract the "Male" in the 5th line but I don't know how to do it. Can anyone help? 
I tried " gen = soup.find('span', class_='tab').string" but it doesn't work. </p>

<p>You don't need to search every tag, you can find the span where the <code>text="GENDER"</code> and get the text from the parent <code>p</code> setting <em>resursive=False</em> to only get the parent text:</p>

<pre><code>In [4]: from bs4 import BeautifulSoup

In [5]: h = """&lt;div&gt;
   ...: &lt;p class="tabbed" style="margin-top:2px;"&gt;&lt;span class="tab"&gt;&lt;strong&gt;LANGUAGES&lt;/strong&gt;&lt;/span&gt;Cantonese&lt;/p&gt;
   ...: &lt;p class="tabbed" style="margin-top:2px;"&gt;&lt;span class="tab"&gt;&lt;/span&gt;English&lt;/p&gt;
   ...: &lt;p class="tabbed" style="margin-top:2px;"&gt;&lt;span class="tab"&gt;&lt;/span&gt;Putonghua&lt;/p&gt;
   ...: &lt;p class="tabbed"&gt;&lt;span class="tab"&gt;&lt;strong&gt;GENDER&lt;/strong&gt;&lt;/span&gt;Male&lt;/p&gt;
   ...:    &lt;/div&gt;"""

In [6]: soup = BeautifulSoup(h,"html.parser")

In [7]: soup.find("span","tab", text="GENDER").parent.find(text=True,recursive=False)
Out[7]: u'Male'
</code></pre>

<p>Or just using the span without the class name:</p>

<pre><code>In [8]: soup.find("span",text="GENDER").parent.find(text=True,recursive=False)
Out[8]: u'Male'
</code></pre>

python html text tags beautifulsoup