Strip span from h1 in Python (Beautiful soup)

Viewed 111

I'm unable to delete the span from the H1 - I need just the H1 text without the text within the span:

page = requests.get("https://www.bbc.co.uk/weather/524901")
soup = BeautifulSoup(page.content, 'html.parser')
weather_desc_today = soup.find(class_="wr-day__weather-type-description").get_text()
weather_location = soup.find('h1').text
print (weather_location)

Outputs:

Moscow - Weather warnings issued

When I need just 'Moscow'

Here's the HTML:

<h1 id="wr-location-name-id" tabindex="-1" class="wr-c-location__name gel-paragon">Moscow<span class="gs-u-vh wr-c-warnings-issued"> - <!-- -->Weather warnings issued</span></h1>
1 Answers

You can use find.

weather_location = soup.find('h1').find(text=True)

OUT: Moscow
Related