Davi Leo
Davi Leo

Reputation: 84

Get the text which is found inside a nested Div tag using python BeautifulSoup

I am trying to scrape the text between nested div but unable to get the text(TEXT HERE).The text is found inside the nested div. text here. So as you see below i want to print out the text(TEXT HERE) which is found inside all those 'div',as the text is not inside a 'p' tag i was unable to print the text. I am using BeautifulSoup to extract the text.When i run the code below ,it does not print out anything. The structure of the 'div' is

<div class="_333v _45kb".....
    <div class="_2a_i" ...............
        <div class="_2a_j".......</div>
        <div class="_2b04"...........
            <div class="_14v5"........
                <div class="_2b06".....
                    <div class="_2b05".....</div>
                    <div id=............>**TEXT HERE**</div>
                </div>
            </div>
        </div>
    </div>
</div>

My code:

theurl = "here URL"
thepage = urllib.request.urlopen(theurl)
soup = BeautifulSoup(thepage, "html.praser")
comm_list = soup.findAll('div', class_="_333v _45kb")
for lists in comm_list:
    print(comm_list.find('div').text)

Upvotes: 0

Views: 896

Answers (1)

buran
buran

Reputation: 14218

Beacuse OP continue to not provide enough information, here is sample

from bs4 import BeautifulSoup

html = '''
<div class="foo">
    <div class="bar">
        <div class="spam">Some Spam Here</div>
        <div id="eggs">**TEXT HERE**</div>
    </div>
</div>
'''

soup = BeautifulSoup(html, 'html.parser')

# This will print all the text
div = soup.find('div', {'class':'foo'})
print(div.text)


print('\n----\n')
# if other divs don't have id
for div in soup.findAll('div'):
    if div.has_attr('id'):
        print(div.text)

output

Some Spam Here
**TEXT HERE**


---------
**TEXT HERE**

Upvotes: 1

Related Questions