There's more...

As stated, it's best to use the content-type header to determine an extension for storing the file locally.  There are other techniques than what is provided here, but this is the easiest.

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.