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'?
CodePudding user response:
You could use the text
and recursive
parameters in find
:
out = soup.find('p').find(text=True, recursive=False).rstrip()
Output:
'Hello'