Python学习笔记31:图片URL批量转存到本地

一、数据说明

有几千张图片地址,需要下载到本地,数据形式如下:

二、urllib请求方式批量获取

import os
from urllib import request,error
import requests
import requests


headers = {
    'User_Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'
}

src = 'https://pubfile.xxxxxx.com.cn/'


with open('身份证图片链接.txt','r') as f:
    i = 1
    for path in f:
        url = os.path.join(src,path)
        # image_name = url.strip().split('/')[-1]
        image_name = str(i).zfill(4)+ '.jpg'
        try:
            req = request.Request(url=url, headers= headers)
            response = request.urlopen(req)
            image_path = os.path.join(os.getcwd() + '/images/' + image_name)
            with open(image_path, 'wb') as f:
                f.write(response.read())
        except error.URLError as e:  
            pass
        i += 1
        print('第%s张,save %s to %s sussfully'%(i,image_name,image_path))

结果输出:

 

posted @ 2019-07-23 17:01  zheng1076  阅读(1088)  评论(0编辑  收藏  举报