Narrow in a bit more on a particular bit of text using beautifulsoup

Viewed 36

I'm trying to get the river level from here https://flood-warning-information.service.gov.uk/station/8108

I'm using this script

import requests
from bs4 import BeautifulSoup

url = "https://flood-warning-information.service.gov.uk/station/8108"

r = requests.get(url)

soup = BeautifulSoup(r.content, "lxml")

g_data = soup.find_all("header", {"intro"})

print g_data[0].text

Which gives me

River Skerne at John St Darlington

Latest recorded level 0.72m at 10:30am Thursday 8 October 2020.

which is dandy, but all I really want is the level itself.

Anyway to further zone in on the 0.72m bit?

Thanks!

1 Answers

You can get text from first <strong> element inside element with class="intro":

import requests
from bs4 import BeautifulSoup


url = 'https://flood-warning-information.service.gov.uk/station/8108'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

print(soup.select_one('.intro strong').text)

Prints:

0.72m
Related