download-images.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import requests
  2. import time
  3. import concurrent.futures
  4. img_urls = [
  5. 'https://images.unsplash.com/photo-1516117172878-fd2c41f4a759',
  6. 'https://images.unsplash.com/photo-1532009324734-20a7a5813719',
  7. 'https://images.unsplash.com/photo-1524429656589-6633a470097c',
  8. 'https://images.unsplash.com/photo-1530224264768-7ff8c1789d79',
  9. 'https://images.unsplash.com/photo-1564135624576-c5c88640f235',
  10. 'https://images.unsplash.com/photo-1541698444083-023c97d3f4b6',
  11. 'https://images.unsplash.com/photo-1522364723953-452d3431c267',
  12. 'https://images.unsplash.com/photo-1513938709626-033611b8cc03',
  13. 'https://images.unsplash.com/photo-1507143550189-fed454f93097',
  14. 'https://images.unsplash.com/photo-1493976040374-85c8e12f0c0e',
  15. 'https://images.unsplash.com/photo-1504198453319-5ce911bafcde',
  16. 'https://images.unsplash.com/photo-1530122037265-a5f1f91d3b99',
  17. 'https://images.unsplash.com/photo-1516972810927-80185027ca84',
  18. 'https://images.unsplash.com/photo-1550439062-609e1531270e',
  19. 'https://images.unsplash.com/photo-1549692520-acc6669e2f0c'
  20. ]
  21. t1 = time.perf_counter()
  22. def download_image(img_url):
  23. img_bytes = requests.get(img_url).content
  24. img_name = img_url.split('/')[3]
  25. img_name = f'{img_name}.jpg'
  26. with open(img_name, 'wb') as img_file:
  27. img_file.write(img_bytes)
  28. print(f'{img_name} was downloaded...')
  29. with concurrent.futures.ThreadPoolExecutor() as executor:
  30. executor.map(download_image, img_urls)
  31. t2 = time.perf_counter()
  32. print(f'Finished in {t2-t1} seconds')