'Finding text in p-tag which includes a span-tag with beautifulsoup

Using beautifulsoup I need to find the text inside a <p>-tag, which has a <span>-tag inside of it, but I am only interested in the text from the <p>-tag. And just finding <p>-tags gives way too many result, so I want to limit them by the spans.

Here is an example of what I want to achieve:

text_example = '<p>Hello <span>World</span></p>'
soup = BeautifulSoup(text_example, 'html.parser')

print(soup.find('p').string) # Prints None

print(soup.find('p').find('span').string) # Prints World

How do I get the value: 'Hello'?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source