How it works

The .contentype property is implemented as follows:

@propertydef contenttype(self):    self.ensure_response()    return self._response.headers['content-type']

The .headers property of the _response object is a dictionary-like class of headers.  The content-type key will retrieve the content-type specified by the server.  This call to the ensure_response() method simply ensures that the .read() function has been executed.

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.