数据源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()
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
end_time = time.time()
duration = end_time - start_time
print('耗时',duration,'秒')
命令行执行python down.py即可
当然也可以使用第三方模块requests,需要另行安装
Python request与requests比较
pip3 install requests
版权声明:本文为buyueliuying原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。