BeautifulSoup中是否有一个相同的InnerText?

LA_*_*LA_ 30 python beautifulsoup

使用以下代码:

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

我得到以下html:

<div id="ctl00_ContentPlaceHolder1_Item65404" class="flagPageTitle" style=" ">
<span></span><p>Some text here</p>
</div>
Run Code Online (Sandbox Code Playgroud)

如何在Some text here没有任何标签的情况下获得?是否有InnerText等效BeautifulSoup

Phi*_*per 36

所有你需要的是:

result = soup.find('div', {'class' :'flagPageTitle'}).text
Run Code Online (Sandbox Code Playgroud)


Rob*_*ers 6

您可以搜索<p>并获取其文本:

soup = BeautifulSoup.BeautifulSoup(page.read(), fromEncoding="utf-8")
result = soup.find('div', {'class': 'flagPageTitle'})
result = result.find('p').text
Run Code Online (Sandbox Code Playgroud)


voi*_*hos 5

您可以使用findAll(text=True)仅查找文本节点。

result = u''.join(result.findAll(text=True))
Run Code Online (Sandbox Code Playgroud)