Skip to content

Instantly share code, notes, and snippets.

@SpotlightKid
Forked from Chalarangelo/unscrape.py
Last active November 2, 2017 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpotlightKid/b12efbe21aeb0e1b7f036e68999be138 to your computer and use it in GitHub Desktop.
Save SpotlightKid/b12efbe21aeb0e1b7f036e68999be138 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import requests
import time
from io import BytesIO
from os.path import join
from selenium import webdriver
from PIL import Image
url = "https://unsplash.com"
os.makedirs('images', exist_ok=True)
driver = webdriver.Firefox()
driver.get(url)
driver.execute_script("window.scrollTo(0,1000);")
time.sleep(5)
image_elements = driver.find_elements_by_css_selector("#gridMulti img")
for i, image_element in enumerate(image_elements):
image_url = image_element.get_attribute("src")
# Send an HTTP GET request, get and save the image from the response
print("Fetching image {}/{}...".format(i+1, len(image_elements)))
image_object = requests.get(image_url)
image = Image.open(BytesIO(image_object.content))
filename = "image-{:02}.{}".format(i + 1, image.format.lower())
print("Saving image to '{}'...".format(filename))
image.save(join("images", filename), image.format)
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment