脚本批量下载文件并指定文件名

数据源urls.txt文件格式如下,每行一个文件名➕一个文件地址,中间用空格隔开

30035.png https://test.cn/uploads/ae8b2718b067a03c2b6e.png
30037.png https://test.cn/uploads/941c916a4a2489b946db.png
30050.png https://test.cn/uploads/56a01998b98d44cc68b5.png
30052.png https://test.cn/uploads/285145b917451557dc44.png
30053.png https://test.cn/uploads/0781117da06d1381428d.png

Shell脚本down.sh

#!/bin/bash
while read file_name file_url
do
    if [ -f "./img/${file_name}" ]
    then
        echo "文件${file_name}已存在"
    else
        wget -O ./img/${file_name} -c ${file_url}
    fi
    
done < urls.txt

命令行执行./down.sh即可

Python脚本down.py

#-*- coding:utf-8 –*-
from urllib import request
import ssl
import os
import time

ssl._create_default_https_context = ssl._create_unverified_context

start_time = time.time()

def download():
    try:
        file = open("urls.txt",'r')
        while True:
            line = file.readline()
            if line:
                arr = line.split(' ')
                file_name = arr[0]
                file_url = arr[1]
                if os.path.exists('./img/' + file_name):
                    print('文件' + file_name +'已存在')
                else:
                    request.urlretrieve(file_url,'./img/' + file_name)
            else:
                break
            
    except Exception as e:
        print(e)
        print('发生错误重新请求')
        download()
    finally:
        file.close()
        end_time = time.time()
        duration = end_time - start_time
        print('处理完成,一共耗时',duration,'秒')
        
download()

命令行执行python down.py即可

当然也可以使用第三方模块requests,需要另行安装
Python request与requests比较

pip3 install requests

如果文件数量比较多,可以通过分割成几个小的文本文件,开启多个Python进程一起下载
Python大文本文件按行数分割成多个小文本文件
shell向python传递参数

创建bd.py

#-*- coding:utf-8 –*-
from urllib import request
import ssl
import os
import time

import argparse
# 新建参数解释器对象
parser = argparse.ArgumentParser()
# 添加参数
parser.add_argument('--sourceFile')
# 添加参数,注明参数类型
#parser.add_argument('--count',type=int)
# 参数赋值,也可以通过终端赋值
args = parser.parse_args()


ssl._create_default_https_context = ssl._create_unverified_context

start_time = time.time()

def download(sourceFile):
    try:
        file = open(sourceFile,'r')
        while True:
            line = file.readline()
            if line:
                arr = line.split(' ')
                file_name = arr[0]
                file_url = arr[1]
                if os.path.exists('./img/' + file_name):
                    print('文件' + file_name +'已存在')
                else:
                    request.urlretrieve(file_url,'./img/' + file_name)
            else:
                break
            
    except Exception as e:
        print(e)
        print('发生错误重新请求')
        download()
    finally:
        file.close()
        end_time = time.time()
        duration = end_time - start_time
        print('处理完成,一共耗时',duration,'秒')
        
download(args.sourceFile)

urls.txt分割成10个小文本文件
并行执行开启10个Python进程加速下载

创建bitch_down.sh,内容如下

#!/bin/sh

python bd.py --sourceFile "urls_part1.txt" & 
python bd.py --sourceFile "urls_part2.txt" & 
python bd.py --sourceFile "urls_part3.txt" & 
python bd.py --sourceFile "urls_part4.txt" & 
python bd.py --sourceFile "urls_part5.txt" & 
python bd.py --sourceFile "urls_part6.txt" & 
python bd.py --sourceFile "urls_part7.txt" & 
python bd.py --sourceFile "urls_part8.txt" & 
python bd.py --sourceFile "urls_part9.txt" & 
python bd.py --sourceFile "urls_part10.txt"

执行

./bitch_down.sh

在这里插入图片描述

### 关于 UniApp 框架推荐资源与教程 #### 1. **Uniapp 官方文档** 官方文档是最权威的学习资料之一,涵盖了从基础概念到高级特性的全方位讲解。对于初学者来说,这是了解 UniApp 架构技术细节的最佳起点[^3]。 #### 2. **《Uniapp 从入门到精通:案例分析与最佳实践》** 该文章提供了系统的知识体系,帮助开发者掌握 Uniapp 的基础知识、实际应用以及开发过程中的最佳实践方法。它不仅适合新手快速上手,也能够为有经验的开发者提供深入的技术指导[^1]。 #### 3. **ThorUI-uniapp 开源项目教程** 这是一个专注于 UI 组件库设计实现的教学材料,基于 ThorUI 提供了一系列实用的功能模块。通过学习此开源项目的具体实现方式,可以更好地理解如何高效构建美观且一致的应用界面[^2]。 #### 4. **跨平台开发利器:UniApp 全面解析与实践指南** 这篇文章按照章节形式详细阐述了 UniApp 的各个方面,包括但不限于其工作原理、技术栈介绍、开发环境配置等内容,并附带丰富的实例演示来辅助说明理论知识点。 以下是几个重要的主题摘选: - **核心特性解析**:解释了跨端运行机制、底层架构组成及其主要功能特点。 - **开发实践指南**:给出了具体的页面编写样例代码,展示了不同设备间 API 调用的方法论。 - **性能优化建议**:针对启动时间缩短、图形绘制效率提升等方面提出了可行策略。 ```javascript // 示例代码片段展示条件编译语法 export default { methods: { showPlatform() { console.log(process.env.UNI_PLATFORM); // 输出当前平台名称 #ifdef APP-PLUS console.log('Running on App'); #endif #ifdef H5 console.log('Running on Web'); #endif } } } ``` #### 5. **其他补充资源** 除了上述提到的内容外,还有许多在线课程视频可供选择,比如 Bilibili 上的一些免费系列讲座;另外 GitHub GitCode 平台上也有不少优质的社区贡献作品值得借鉴研究。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值