获取 upsplash 壁纸图片

Posted on Posted in python
文章目录[隐藏]

获取 upsplash 壁纸图片

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8

__author__ = "liuhonghe"
__email__  = "i@liuhonghe.me"

from fake_useragent import UserAgent
from contextlib import closing
import sys, requests
from urllib.parse import urlparse,parse_qs

storage_dir = './'
unsplash_api = 'https://source.unsplash.com/random/1920x1080'
ua = UserAgent().random

headers = {"User-Agent": ua}
s = requests.Session()
unsplash_imgs_api_res = s.get(unsplash_api, headers = headers, stream = True)

url_res = urlparse(unsplash_imgs_api_res.url)

img_name = url_res.path.lstrip('/')
suffix = parse_qs(url_res.query)['fm'][0]
fullImgName = img_name + '.' + suffix

with closing(unsplash_imgs_api_res) as response:
    with open(storage_dir + fullImgName, 'wb') as fd:
        for chunk in response.iter_content(128):
            fd.write(chunk)

if __name__ == '__main__':
    pass
» 转载请注明来源:呢喃 » 获取 upsplash 壁纸图片

Leave a Reply

Your email address will not be published. Required fields are marked *

20 + 18 =