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

    结果输出:

  • 相关阅读:
    ByteArrayInputStream(字节数组输入流) 示例
    ASP.NET 页面对象模型
    HTML常用标记
    [elementui]上线iconfont乱码
    time与timeEnd测试运行时间
    放大镜效果
    canvas
    [css] 样式列表
    千分符
    centos源失败
  • 原文地址:https://www.cnblogs.com/zheng1076/p/11232919.html
Copyright © 2020-2023  润新知