'Scraping <span> text</span> with BeautifulSoup and urllib

I want to scrape 2015 from below HTML: HTML code

I use the below code but am only able to scrape "Annee"

soup.find('span', {'class':'optionLabel'}).get_text()

Can someone please help?

I am a new learner.



Solution 1:[1]

Simply try to find its next span that holds the text you wanna scrape:

soup.find('span',  {'class':'optionLabel'}).find_next('span').get_text()

or css selectors with adjacent sibling combinator:

soup.select_one('span.optionLabel + span').get_text()
Example
html='''
<span class="optionLabel"><button>Année</button</span> :
<span>2015</span>'''
from bs4 import BeautifulSoup 

soup = BeautifulSoup(html)
soup.find('span', {'class':'optionLabel'}).find_next('span').get_text()
Output
2015

Sources

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

Source: Stack Overflow

Solution Source
Solution 1