Issue
I want to get the text of an element combined with the text of it's children elements. The element that I have looks like this.
<td>
<a> This </a>
" is placement "
<span>
<span> text </span>
</span>
" to demonstrate "
<span>
<span> my point </span>
</span>
</td>
my initial though was to do this
children = d.findChildren()
description = ''
for child in children:
if child.text:
description += child.text
This just returns the text 'This text my point' because it ignores the text in the main element. How would I get the complete sentence 'This is placement text to demonstrate my point' ?
Solution
You can use .text
or .get_text()
. For example:
from bs4 import BeautifulSoup
html_text = '''\
<td>
<a> This </a>
" is placement "
<span>
<span> text </span>
</span>
" to demonstrate "
<span>
<span> my point </span>
</span>
</td>'''
soup = BeautifulSoup(html_text, 'html.parser')
print(soup.td.get_text(strip=True, separator=' '))
Prints:
This " is placement " text " to demonstrate " my point
Answered By - Andrej Kesely
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.