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?

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.