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

情到浓时终转凉″ 提交于 2021-02-10 06:31:51

问题


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:


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


来源:https://stackoverflow.com/questions/64260092/narrow-in-a-bit-more-on-a-particular-bit-of-text-using-beautifulsoup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!