通過圖片的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()))