# This example is not good for production use from urllib.request import urlopen, URLError from html.parser import HTMLParser from functools import cache class Parser(HTMLParser): def __init__(self): super().__init__() self.versions = [] self.feed(urlopen("https://www.python.org/ftp/python").read().decode()) self.versions.sort(key=self._by_version) def handle_starttag(self, tag, content): content = dict(content) match tag: case "a" if "href" in content and content["href"][0].isdigit(): self.versions.append(content["href"].rstrip("/")) @staticmethod def _by_version(value): return list(map(int, value.split("."))) @cache def get_latest_python_version(): try: parser = Parser() except URLError: raise RuntimeError("Could not request python.org") # removing latest version, which should be always # a development version return parser.versions[-2] get_latest_python_version()