On 19/05/2026 09:09, Ralph Corderoy wrote:
Hi Graeme,
I need to find the value of the class attribute, which could be
"marine-card" or "marine-card warning" but
print(section.contents) starts with the <h2 line, and
print(section.contents[0] prints only a blank line.
I think you need to know about a Tag's attributes.
https://www.crummy.com/software/BeautifulSoup/bs4/doc/#Tag.attrs
My knowledge of Python is out of date, and I've not used BeautifulSoup
before, but here's some working code.
$ cat parse
#! /usr/bin/python3
import bs4
import requests
import warnings
def main():
warnings.simplefilter('ignore', category=bs4.GuessedAtParserWarning)
url =
'https://weather.metoffice.gov.uk/specialist-forecasts/coast-and-sea/inshore-waters-forecast'
resp = requests.get(url)
html = bs4.BeautifulSoup(resp.content)
sect = html('section', id='inshore-waters-7', limit=2)
assert len(sect) == 1, sect
sect = sect[0]
cls = sect['class']
print(sorted(cls))
main()
$
$ ./parse
['marine-card', 'warning']
$
Ralph, thank you, just the job. I wonder why your sect = sect[0] works
when mine didn't?
Anyway, thanks again.
Graeme
--
Next meeting: Online, Jitsi, Tuesday, 2026-06-02 20:00
Check to whom you are replying
Meetings, mailing list, IRC, ... https://dorset.lug.org.uk
New thread, don't hijack: mailto:[email protected]