Python中h1的条带跨度(美丽的汤)

Python中h1的条带跨度(美丽的汤),python,beautifulsoup,Python,Beautifulsoup,我无法从H1中删除范围-我只需要H1文本,而不需要范围内的文本: 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 = sou

我无法从H1中删除范围-我只需要H1文本,而不需要范围内的文本:

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)

产出:

Moscow - Weather warnings issued
当我只需要“莫斯科”的时候

以下是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>
莫斯科-发布天气警告

您可以使用
查找

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

OUT: Moscow