title: python: image corrupted when downloading via urllib
body: <p>I have an old project which is downloading images via urllib.</p>
  
  <p>Sometimes, the images which are downloaded are corrupted.</p>
  
  <p>Take the below image for example,</p>
  
  <p><a href="https://images.allsaints.com/products/900/WO053N/2824/WO053N-2824-1.jpg" rel="nofollow noreferrer">https://images.allsaints.com/products/900/WO053N/2824/WO053N-2824-1.jpg</a></p>
  
  <p>When I try to open it in Image Viewer, I get</p>
  
  <pre><code>Error interpreting JPEG image file (Not a JPEG file: starts with 0x3c 0x21)
  </code></pre>
  
  <p>Turns out, it is an html file.</p>
  
  <p>So, I read this answer,</p>
  
  <p><a href="https://stackoverflow.com/questions/29047316/how-to-test-if-a-webpage-is-an-image">How to test if a webpage is an image</a></p>
  
  <p>So, I checked headers for this image,</p>
  
  <pre><code>&gt;&gt;&gt; b = requests.get('https://images.allsaints.com/products/900/WO053N/2824/WO053N-2824-1.jpg')
  &gt;&gt;&gt; b.headers.get('content-type')
  'image/jpeg'
  </code></pre>
  
  <p>It shows in the headers that it is an image/jpeg, but actually it is HTML page.</p>
  
  <p>So, How can I properly check that whether a url is an image OR HTML?</p>