Python3学习之路-通过url下载文件

通过图片的url下载到本地目录保存。
模块:urllib.request
代码如下:

# coding=utf-8
import time
from urllib import request


def download(URL):
    req = request.Request(URL)
    res = request.urlopen(req)
    get_img=res.read()
    with open('F:\\csdn.jpg','wb')as f:
        f.write(get_img)
        print('download success')

if __name__ == '__main__':
    url = 'https://csdnimg.cn/pubfooter/images/csdn-cxrs.png'
    print(u'download starts at ' + time.strftime('%Y-%m-%d %H.%M.%S', time.localtime()))
    download(url)
    print(u'download ends at ' + time.strftime('%Y-%m-%d %H.%M.%S', time.localtime()))

你可能感兴趣的:(笔记)