Skip to content

Instantly share code, notes, and snippets.

@suxiaogang
Forked from cnDelbert/unsplash.py
Last active August 29, 2015 14:17
Show Gist options
  • Save suxiaogang/2db6ca274c04c12be75e to your computer and use it in GitHub Desktop.
Save suxiaogang/2db6ca274c04c12be75e to your computer and use it in GitHub Desktop.
# -*- coding:utf-8 -*-
__author__ = 'Delbert'
# Using Python3
# beautifulsoup4 is needed.
from bs4 import BeautifulSoup
from urllib import request
import os
def main():
begin_page = int(input("From: "))
end_page = int(input("To: "))+1
for page in range(begin_page, end_page):
url = "https://unsplash.com/?page=%s" %page
print(url)
data = BeautifulSoup(request.urlopen(url))
a_link = data.find_all("a", text="Download")
for d_link in a_link:
id = str(d_link).split('/')[2]
if(os.path.exists("./unsplash/"+id+".jpg") and (os.path.getsize("./unsplash/"+id+".jpg") > 0)):
continue
else:
print("Downloading pic: "+id+".jpg ... ", end="")
f = open("./unsplash/"+id+".jpg", "wb")
id_url = "https://unsplash.com/photos/"+id+"/download"
f.write(request.urlopen(id_url).read())
f.close()
print("Done")
if __name__ == '__main__':
if(not os.path.exists('./unsplash/')):
os.mkdir("./unsplash/")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment