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()))


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM