I would like to extract a string from a HTML source with only beautifulsoup. I am trying to extract: “1 van de maximaal 3 actieve reacties” from the following HTML:
JavaScript
x
2
1
<span class="titel ng-scope" translate="ReactiesTitel-Titel-actieve" translate-values="getTranslationValues()">1 van de maximaal 3 actieve reacties</span>
2
My current code retrieves the entire span class, but I cannot find out how I can only extract the string, without the use of .split or some sort of string manipulation.
Current code:
JavaScript
1
5
1
html = driver.page_source
2
soup = BeautifulSoup(html, 'html.parser')
3
x = soup.find('span', {'class':'titel ng-scope'})
4
print(x)
5
Advertisement
Answer
JavaScript
1
2
1
from bs4 import BeautifulSoup
2
If you have:
JavaScript
1
3
1
html = '<span class="titel ng-scope" translate="ReactiesTitel-Titel-actieve" translate-values="getTranslationValues()">1 van de maximaal 3 actieve reacties</span>'
2
soup = BeautifulSoup(html, 'html.parser')
3
You can get 1 van de maximaal 3 actieve reacties
by:
JavaScript
1
2
1
soup.text
2
A similar thread, where I got the idea from is: How to get text from span tag in BeautifulSoup.