reorx
10/29/2014 - 3:05 AM

unsplash.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import requests
from bs4 import BeautifulSoup


BASE_URL = 'https://unsplash.com'


def get_parse_page(i):
    page_url = '%s/?page=%s' % (BASE_URL, i)
    print 'Fetching url', page_url

    resp = requests.get(page_url)
    soup = BeautifulSoup(resp.content)

    hrefs = []

    for div in soup.find_all('div', class_='photo'):
        a = div.find('a')
        hrefs.append(a['href'])

    return hrefs


def download_image(url, file_prefix):
    print 'Downloading', url
    resp = requests.get(url)
    ct = resp.headers.get('content-type', 'image/jpeg')
    try:
        ext = '.' + ct.split('/')[1]
    except:
        ext = ''

    file_name = file_prefix + ext
    file_path = os.path.join('pictures', file_name)
    with open(file_path, 'w') as f:
        f.write(resp.content)


def main():
    # 9 pages
    file_count = 0
    for i in xrange(1, 10):
        hrefs = get_parse_page(i)

        for href in hrefs:
            download_image(BASE_URL + href, '20141016-%s' % file_count)
            file_count += 1


if __name__ == '__main__':
    main()