How it works

In the constructor for URLUtility, there is a call to urlib.parse.urlparse.  The following demonstrates using the function interactively:

>>> parsed = urlparse(const.ApodEclipseImage())>>> parsedParseResult(scheme='https', netloc='apod.nasa.gov', path='/apod/image/1709/BT5643s.jpg', params='', query='', fragment='')

The ParseResult object contains the various components of the URL.  The path element contains the path and the filename.  The call to the .filename_without_ext property returns just the filename without the extension:

@propertydef filename_without_ext(self):    filename = os.path.splitext(os.path.basename(self._parsed.path))[0]    return filename

The call to os.path.basename returns only the filename portion of the path ...

Get Python Web Scraping Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.