title: Python script to download all images from a website to a specified folder with BeautifulSoup
body: <p>I found <a href="https://stackoverflow.com/a/46143318">this post</a> and wanted to modify the script slightly to download the images to a specific folder. My edited file looks like this:</p>
  
  <pre><code>import re
  import requests
  from bs4 import BeautifulSoup
  import os
  
  site = 'http://pixabay.com'
  directory = "pixabay/" #Relative to script location
  
  response = requests.get(site)
  
  soup = BeautifulSoup(response.text, 'html.parser')
  img_tags = soup.find_all('img')
  
  urls = [img['src'] for img in img_tags]
  
  for url in urls:
      #print(url)
      filename = re.search(r'/([\w_-]+[.](jpg|gif|png))$', url)
  
      with open(os.path.join(directory, filename.group(1)), 'wb') as f:
          if 'http' not in url:
              url = '{}{}'.format(site, url)
          response = requests.get(url)
          f.write(response.content)
  </code></pre>
  
  <p>This seems to work fine for <strong>pixabay</strong>, but if I try a different site like <strong>imgur</strong> or <strong>heroimages</strong>, it doesn't seem to work.
  If I replace the site declaration with</p>
  
  <pre><code>site = 'http://heroimages.com/portfolio'
  </code></pre>
  
  <p>nothing is downloaded. The print statement (when uncommented) doesn't print anything, so I'm guessing it's not finding any image tags? I'm not sure.</p>
  
  <p>On the other hand, if I replace site with</p>
  
  <pre><code>site = 'http://imgur.com'
  </code></pre>
  
  <p>I sometimes get a </p>
  
  <pre><code>AttributeError: 'NoneType' object has no attribute 'group'
  </code></pre>
  
  <p>or, if the images do download, I can't even open them because I get the following error:
  <a href="https://i.stack.imgur.com/0QEmS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0QEmS.png" alt="not supported file format"></a></p>
  
  <p>Also worth noting, right now the script <strong>requires</strong> the folder specified by directory to exist. I plan on changing it in the future so that the script creates the directory, if it does not exist already.</p>