title: Scrapy: Check if response is an image
body: <blockquote>
    <p>I need check if response is an image.</p>
  </blockquote>
  
  <p>For requirements of the work I need to generate the url of the photos that can exist or no and record the url that contains an image.</p>
  
  <p>When the url generated doesn't show a photo the response of the website is an html when the body is:</p>
  
  <pre><code>&lt;body&gt;No File Found&lt;/body&gt; 
  </code></pre>
  
  <p>also the <code>response.status =200</code> </p>
  
  <p>The response header doesn't have a valuable info for both results with image and <code>No File Found</code></p>
  
  <pre><code>For instance
  HTTP/1.1 200 OK
  Cache-Control: no-cache, no-store, must-revalidate
  Pragma: no-cache
  Transfer-Encoding: chunked
  Expires: 0
  Server: Microsoft-IIS/8.5
  X-Powered-By: ASP.NET
  X-Frame-Options: AllowAll
  Access-Control-Allow-Origin: *
  Access-Control-Allow-Headers: *
  Date: Tue, 13 Aug 2019 01:44:40 GMT
  </code></pre>
  
  <p>The way that I found to check if the response is an image  for this case was:</p>
  
  <pre><code>        try :
              no_file_found = response.xpath("/html/body[contains(., 'No File Found')]")
          except:
              photo_url = response.url
              photo = PhotoItem()
  
              photo['id'] = id
              photo['url'] = photo_url
  
              yield photo
  </code></pre>
  
  <p>Because When the response is an image the line</p>
  
  <pre><code>no_file_found = response.xpath("/html/body[contains(., 'No File Found')]")
  </code></pre>
  
  <p>throw this exception:</p>
  
  <pre><code>raise NotSupported("Response content isn't text")
  </code></pre>
  
  <p>I know that this isn't an elegant solution , but for this context it works</p>
  
  <p><strong>Question</strong></p>
  
  <p>My question is If there is another way more elegant to solve this problem, that not use <code>try</code> to solve that.</p>
  
  <p>Notice that I don't need to download the image just need to record the valid url</p>
  
  <p>Any suggestion is welcome.</p>
  
  <p>Thanks in advance!!!</p>