PowerShell 实现批量下载文件
简介
批量文件下载器 PowerShell 版,类似于迅雷批量下载功能,且可以破解 Referer 防盗链
源代码
[int]$script:completed = 0 # 下载完成数量 [int]$script:succeed = 0 # 下载成功数量# 开始下载(普通方法) function StartDownload {param([array]$urlList, [string]$path, [string]$referer)$last = $urlList.Count$watch = Measure-Command {for($i = 0; $i -lt $last; $i++) {DownloadItem -url $urlList[$i] -path $path -referer $refererStart-Sleep -Milliseconds 200 # 延迟0.2秒 }}$failed = $script:completed - $succeed$elapsed = [Math]::Round($watch.TotalMilliseconds/1000, 2) # 总计耗时(秒)Write-Output ""Write-Host "总共下载 $script:completed,成功 $script:succeed,失败 $failed,耗时 $elapsed s" -ForegroundColor Red -BackgroundColor Yellow$script:completed = 0$script:succeed = 0 }# 下载单个文件 function DownloadItem {param([string]$url, [string]$path, [string]$referer)$url_file = $url.Substring($url.LastIndexOf('/') + 1);if($referer.Contains("(*)")) {$referer = $referer -replace "\(\*\)", $url}try {$tmpFileName = [System.IO.Path]::GetTempFileName()$destFileName = [System.IO.Path]::Combine($path, $url_file)$watch = Measure-Command {# 下载文件到临时文件夹Invoke-WebRequest -Uri $url -Method Get -Headers @{"Referer"=$referer} -UserAgent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36" -TimeoutSec 120 -OutFile $tmpFileName# 将临时文件移动到目标文件夹Move-Item -Path $tmpFileName -Destination $destFileName -Force}$script:succeed += 1$fileLength =[Math]::Ceiling((Get-Item -LiteralPath $destFileName).Length / 1024.0)$elapsed = [Math]::Round($watch.TotalMilliseconds)# 下载成功!12.jpg - 115KB/2356msWrite-Host "下载成功!$url_file - $fileLength KB/$elapsed ms" -ForegroundColor Green} catch {Write-Error $PSItem.ToString()} finally {$script:completed += 1} }# 主函数 运行 AppStart 即可启动 function AppStart {Clear-HostWrite-Welcome$urlFormat = ReadInput_Url -message "输入URL(含通配符,例如 http://www.spany.com/2019/(*).jpg)"$start = ReadInput_Integer -message "通配符数字开始(0~200)" -minValue 0 -maxValue 200$end = $start + 200$end = ReadInput_Integer -message "通配符数字结束($start~$end)" -minValue: $start -maxValue $end$len = ReadInput_Integer -message "通配符数字长度(1~5)" -minValue: 1 -maxValue 5$referer = ReadInput_Url -message "输入Referer为破解防盗链(如果Referer中含有通配符(*),则将被当前URL替换,如无须Referer则直接回车)" -defaultValue "https://www.baidu.com/visit"Write-Output ""$urlList = BuildUrlList -urlFormat $urlFormat -start $start -end $end -len $lenif($urlList.Count -gt 0) {Write-Output "URL列表如下:"foreach($url in $urlList) {Write-Output "`t$url"}Write-Output ""if(ReadInput_YesOrNo -message "是否开始下载?(y/n)") {$path = ReadInput_Path -message "输入文件存储目录"Write-Output ""StartDownload -urlList $urlList -path $path -referer $referer}} else {Write-Warning "不能创建URL列表,请核对参数!"}Write-Output "" }AppStart
完整代码: https://gitee.com/codefelix/spany-down-ps.git
如何使用
定位到文件目录,右键 spany-down-ps.ps1 选择“使用 PowerShell 运行”,如提示此系统上禁止运行脚本,可执行命令 Set-ExecutionPolicy -ExecutionPolicy Unrestricted 更改执行策略
然后按照屏幕提示,输入必要参数,启动下载进程,截图演示下载美图录的美女写真集(宅男福利啊!)
如果你直接打开图片或者用迅雷批量下载,都将被 403 Forbidden,因为网站启用了 Referer 防盗链
不过 PowerShell 版还是单线程顺序下载,另一个 C#/.NET Core 版 https://gitee.com/codefelix/spany-down-sharp 采用异步下载,有进度显示,效率更高
转载于:https://www.cnblogs.com/felixnet/p/10745853.html
PowerShell 实现批量下载文件相关推荐
- java批量下载文件为zip包
批量下载文件为zip包的工具类 package com.meeno.trainsys.util;import javax.servlet.http.HttpServletRequest; import ...
- python批量下载文件-Python实现批量下载文件
Python实现批量下载文件 #!/usr/bin/env python # -*- coding:utf-8 -*- from gevent import monkey monkey.patch_a ...
- python批量下载网页文件-Python实现批量下载文件
Python实现批量下载文件 #!/usr/bin/env python # -*- coding:utf-8 -*- from gevent import monkey monkey.patch_a ...
- mysql 线程池 下载_java线程池实现批量下载文件
本文实例为大家分享了java线程池实现批量下载文件的具体代码,供大家参考,具体内容如下 1 创建线程池 package com.cheng.webb.thread; import java.util. ...
- php批量下载TXT中的URL,需求是这样,再不改变目录结构的前提下,通过URL,批量下载文件,我已经写好了一点儿了...
//需求是这样,再不改变目录结构的前提下,通过URL,批量下载文件,我已经写好了一点儿了 //但是有点bug,只能下载最后一个文件 //邱老师解决一下 $file=fopen("q.txt& ...
- 如何用python批量下载数据_Python实现批量下载文件
Python实现批量下载文件 #!/usr/bin/env python # -*- coding:utf-8 -*- from gevent import monkey monkey.patch_a ...
- python 下载文件-Python实现批量下载文件
Python实现批量下载文件 #!/usr/bin/env python # -*- coding:utf-8 -*- from gevent import monkey monkey.patch_a ...
- 批量下载文件,打包成zip压缩包
批量下载文件,用程序打成zip压缩包在下载 前台传来要下载的url数组 @RequestMapping(value = "/download",method = RequestMe ...
- 大数据批量下载文件 代码
大数据批量下载文件 代码 # # 大数据批量下载文件 from multiprocessing import Process import os import pandas as pd import ...
- MinIO按前缀批量下载文件到本地
/*** 按前缀批量下载文件到本地* @param bucketName 存储桶名称* @param prefix 文件前缀* @param saveDir 本地存储路径*/@SneakyThrows ...
最新文章
- 【Tools】MarkDown教程(四)-MarkDown中的UML图
- CodeForces - 1030C Vasya and Golden Ticket(思维)
- Web应用开发中的几个问题
- mysql语句中怎么去重复_mysql中删除重复记录sql语句
- 徐扬:互联网营销下的移动营销
- linux编写python脚本_在ubuntu linux 中编写一个自己的python脚本
- 计算机操作系统的图示,电脑操作系统位数的多种查看方法【图文教程】
- 二开要饭网免费开源源码
- 使用echarts实现半圆饼图
- oa服务器硬件配置,OA办公系统
- 位图的实战场景及源码分析
- 钉钉机器人告警快速实现
- fatal: unable to access ‘https://github.com/thm123/algos.git/‘: Received HTTP code 400 from proxy af
- 消极和积极的道德--给亲爱的安德烈
- HDU 3713 Double Maze
- Linux下gmtime、gmtime_r、localtime、localtime_r函数详解
- 零知识证明的硬件加速
- 真三国无双3 己方士气提升
- LNMP 一键安装包配置 https
- QGIS|三步拥有全国县级行政区划