Is there an InnerText equivalent in BeautifulSoup?

Question:

With the code below:

soup = BeautifulSoup(page.read(), fromEncoding="utf-8")
result = soup.find('div', {'class' :'flagPageTitle'})

I get the following html:

<div id="ctl00_ContentPlaceHolder1_Item65404" class="flagPageTitle" style=" ">
<span></span><p>Some text here</p>
</div>

How can I get Some text here without any tags? Is there InnerText equivalent in BeautifulSoup?

Asked By: LA_

||

Answers:

You can use findAll(text=True) to only find text nodes.

result = u''.join(result.findAll(text=True))
Answered By: voithos

You can search for <p> and get its text:

soup = BeautifulSoup.BeautifulSoup(page.read(), fromEncoding="utf-8")
result = soup.find('div', {'class': 'flagPageTitle'})
result = result.find('p').text
Answered By: Rob Wouters

All you need is:

result = soup.find('div', {'class' :'flagPageTitle'}).text
Answered By: Phil Cooper
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.