Python/bs4: Span inside div tag - text extraction
Question: I'm beating with an text extraction from div tag. The point is that there is a
tag without opening pair, inside the div tag. So if I do this: `raw =
soup.find('div', class_='inside').text` I get only text before the tag.

An example:

    
    
    <div class='inside'><div>sth0</div><div>sth1</div></span><div>sth2<div></div>
    
    soup.find('div', class_='inside').text
    
    >>> sth0  sth1 
    

Do you have an idea how to get a whole text from div tag? Thanks

EDIT (According to Tanmaya Meher, the code above should work, but for me
doesn't so I'm attaching the exact problem

When I run this code:

    
    
    raw = firmHtml.find('div', class_='inside').text
    print raw
    

I get

    
    
    Katalóg   Obchody a veľkoobchod
    

Instead of:

    
    
    Katalóg   Obchody a veľkoobchod   Stavebniny   Izolačný materiál...
    

Here is a cut from my code.

`<div class="inside"><div class="inside2"><a href="/katalog/" style="font-
size:12px" title="Katalóg"><span>Katalóg</span></a> <span class="sipka
s1">&nbsp;</span> <a href="/katalog/obchody-a-velkoobchod/" style="font-
size:12px" itemprop="url" title="Obchody a veľkoobchod"><span itemprop="title"
>Obchody a veľkoobchod</span></a></span> <span class="sipka s1">&nbsp;</span>
<span itemprop="child" itemscope itemtype="http://data-
vocabulary.org/Breadcrumb" ><a href="/katalog/stavebniny_1/" style="font-
size:12px" itemprop="url" title="Stavebniny"><span itemprop="title"
>Stavebniny</span></a></span> <span class="sipka s1">&nbsp;</span> <span
itemprop="child" itemscope itemtype="http://data-vocabulary.org/Breadcrumb"
><a href="/katalog/izolacny-material/" style="font-size:12px" itemprop="url"
title="Izolačný materiál"><span itemprop="title" >Izolačný
materiál</span></a></span> <span class="sipka s1">&nbsp;</span> <span
itemprop="child" itemscope itemtype="http://data-vocabulary.org/Breadcrumb"
><a href="/katalog/protipoziarne-izolacie/" style="font-size:12px"
itemprop="url" title="Protipožiarne izolácie"><span itemprop="title"
>Protipožiarne izolácie</span></a></span> <span class="sipka s1">&nbsp;</span>
Ing. Milan Kalafut</div></div></div><div id="main"><div id="content"><div
itemscope itemtype="http://schema.org/LocalBusiness" class="business-
container"><div id="lavy"><div class="foto s3"><img
src="http://s.aimg.sk/katalog/css/images/nologo.gif" alt="Logo nieje k
dispozícii" /></div><div id="moznosti">`

Maybe I can't see something.


Answer: 
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    from bs4 import BeautifulSoup as BS
    
    html_text = '<div class="inside"><div class="inside2"><a  href="/katalog/" style="font-size:12px"  title="Katalóg"><span>Katalóg</span></a> <span class="sipka s1">&nbsp;</span> <a  href="/katalog/obchody-a-velkoobchod/" style="font-size:12px"  itemprop="url"  title="Obchody a veľkoobchod"><span itemprop="title" >Obchody a veľkoobchod</span></a></span> <span class="sipka s1">&nbsp;</span> <span itemprop="child"  itemscope itemtype="http://data-vocabulary.org/Breadcrumb" ><a  href="/katalog/stavebniny_1/" style="font-size:12px"  itemprop="url"  title="Stavebniny"><span itemprop="title" >Stavebniny</span></a></span> <span class="sipka s1">&nbsp;</span> <span itemprop="child"  itemscope itemtype="http://data-vocabulary.org/Breadcrumb" ><a  href="/katalog/izolacny-material/" style="font-size:12px"  itemprop="url"  title="Izolačný materiál"><span itemprop="title" >Izolačný materiál</span></a></span> <span class="sipka s1">&nbsp;</span> <span itemprop="child"  itemscope itemtype="http://data-vocabulary.org/Breadcrumb" ><a  href="/katalog/protipoziarne-izolacie/" style="font-size:12px"  itemprop="url"  title="Protipožiarne izolácie"><span itemprop="title" >Protipožiarne izolácie</span></a></span> <span class="sipka s1">&nbsp;</span> Ing. Milan Kalafut</div></div></div><div id="main"><div id="content"><div  itemscope itemtype="http://schema.org/LocalBusiness"  class="business-container"><div id="lavy"><div class="foto s3"><img src="http://s.aimg.sk/katalog/css/images/nologo.gif" alt="Logo nieje k dispozícii" /></div><div id="moznosti">'
    
    #html_text = open("a.html",'r').read() #I have commented this, you can do like this too; a.html file contains the same html code as above
    
    firmHtml = BS(html_text)
    raw = firmHtml.find('div', class_='inside').text
    
    print (raw)
    

Output (with both Python 2.7.5 and Python 3.3.2 on Linux):

    
    
    Katalóg   Obchody a veľkoobchod   Stavebniny   Izolačný materiál   Protipožiarne izolácie   Ing. Milan Kalafut
    

