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
?
Answers:
You can use findAll(text=True)
to only find text nodes.
result = u''.join(result.findAll(text=True))
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
All you need is:
result = soup.find('div', {'class' :'flagPageTitle'}).text
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
?
You can use findAll(text=True)
to only find text nodes.
result = u''.join(result.findAll(text=True))
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
All you need is:
result = soup.find('div', {'class' :'flagPageTitle'}).text