robertvari

Simple Image Downloader

Feb 1st, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. import requests
  3. import os
  4.  
  5. download_folder = os.path.join(os.path.dirname(__file__), 'downloads')
  6.  
  7.  
  8. def main(url_list):
  9.     for url in url_list:
  10.         print('-'*100)
  11.         print(f'Get images from {url}...')
  12.         image_links = get_image_links(url)
  13.  
  14.         image_downloader(image_links)
  15.  
  16.  
  17. def get_image_links(url):
  18.     image_links = []
  19.     response = requests.get(url)
  20.     assert response.status_code == 200, 'Site is unreachable'
  21.  
  22.     soup = BeautifulSoup(response.text, 'html.parser')
  23.     images = soup.find_all('img')
  24.     for item in images:
  25.         image_link = item.get('src')
  26.         if not image_link.endswith('.jpg'):
  27.             continue
  28.  
  29.         image_links.append(image_link)
  30.  
  31.     return image_links
  32.  
  33.  
  34. def image_downloader(image_links):
  35.     if not os.path.exists(download_folder):
  36.         os.makedirs(download_folder)
  37.  
  38.     for image_url in image_links:
  39.         file_name = image_url.split('/')[-1]
  40.         print(f'Downloading image: {file_name}')
  41.         file_path = os.path.join(download_folder, file_name)
  42.  
  43.         with open(file_path, 'wb') as f:
  44.             f.write(requests.get(image_url).content)
  45.  
  46.  
  47. url_list = [
  48.     'https://www.dreamstime.com/photos-images/landscape-nature.html',
  49.     'https://www.dreamstime.com/photos-images/animals.html',
  50.     'https://www.dreamstime.com/photos-images/modern-architecture.html',
  51.     'https://www.dreamstime.com/free-technology-images-photos-freecat103',
  52.     'https://www.dreamstime.com/free-travel-images-photos-freecat55',
  53.     'https://www.dreamstime.com/free-arts-architecture-images-photos-freecat69',
  54.     'https://www.dreamstime.com/free-it-computer-images-photos-freecat108',
  55. ]
  56.  
  57. main(url_list)
Add Comment
Please, Sign In to add comment