Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bs4 import BeautifulSoup
- import requests
- import os
- download_folder = os.path.join(os.path.dirname(__file__), 'downloads')
- def main(url_list):
- for url in url_list:
- print('-'*100)
- print(f'Get images from {url}...')
- image_links = get_image_links(url)
- image_downloader(image_links)
- def get_image_links(url):
- image_links = []
- response = requests.get(url)
- assert response.status_code == 200, 'Site is unreachable'
- soup = BeautifulSoup(response.text, 'html.parser')
- images = soup.find_all('img')
- for item in images:
- image_link = item.get('src')
- if not image_link.endswith('.jpg'):
- continue
- image_links.append(image_link)
- return image_links
- def image_downloader(image_links):
- if not os.path.exists(download_folder):
- os.makedirs(download_folder)
- for image_url in image_links:
- file_name = image_url.split('/')[-1]
- print(f'Downloading image: {file_name}')
- file_path = os.path.join(download_folder, file_name)
- with open(file_path, 'wb') as f:
- f.write(requests.get(image_url).content)
- url_list = [
- 'https://www.dreamstime.com/photos-images/landscape-nature.html',
- 'https://www.dreamstime.com/photos-images/animals.html',
- 'https://www.dreamstime.com/photos-images/modern-architecture.html',
- 'https://www.dreamstime.com/free-technology-images-photos-freecat103',
- 'https://www.dreamstime.com/free-travel-images-photos-freecat55',
- 'https://www.dreamstime.com/free-arts-architecture-images-photos-freecat69',
- 'https://www.dreamstime.com/free-it-computer-images-photos-freecat108',
- ]
- main(url_list)
Add Comment
Please, Sign In to add comment