Parsing a URL with urllib to get the filename
When downloading content from a URL, we often want to save it in a file. Often it is good enough to save the file in a file with a name found in the URL. But the URL consists of a number of fragments, so how can we find the actual filename from the URL, especially where there are often many parameters after the file name?
Getting ready
We will again be using the URLUtility
class for this task. The code file for the recipe is 04/02_parse_url.py
.
How to do it
Execute the recipe's file with your python interpreter. It will run the following code:
util = URLUtility(const.ApodEclipseImage()) print(util.filename_without_ext)
This results in the following output:
Reading URL: https://apod.nasa.gov/apod/image/1709/BT5643s.jpg Read 171014 bytes The filename is: BT5643s
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...