Skip to content

Instantly share code, notes, and snippets.

@cnDelbert
Last active November 15, 2017 02:08
Show Gist options
  • Save cnDelbert/96881e8d0c2a9bb072db to your computer and use it in GitHub Desktop.
Save cnDelbert/96881e8d0c2a9bb072db to your computer and use it in GitHub Desktop.
一个简单的unsplash全站下载脚本。 使用Python3及BeautifulSoup4。
# -*- 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()
@iLeyar
Copy link

iLeyar commented Mar 6, 2016

貌似现在用不了了。。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment