PowerShell 實現批量下載文件

簡介git

批量文件下載器 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 $referer
            Start-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/2356ms
        Write-Host "下載成功!$url_file - $fileLength KB/$elapsed ms" -ForegroundColor Green
    } catch {
        Write-Error $PSItem.ToString()
    } finally {
        $script:completed += 1
    }
}

# 主函數 運行 AppStart 便可啓動
function AppStart {
    Clear-Host
    Write-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 $len
    if($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網站

 

如何使用ui

定位到文件目錄,右鍵 spany-down-ps.ps1 選擇「使用 PowerShell 運行」,如提示此係統上禁止運行腳本,可執行命令 Set-ExecutionPolicy -ExecutionPolicy Unrestricted 更改執行策略url

而後按照屏幕提示,輸入必要參數,啓動下載進程,截圖演示下載美圖錄的美女寫真集(宅男福利啊!)spa

若是你直接打開圖片或者用迅雷批量下載,都將被 403 Forbidden,由於網站啓用了 Referer 防盜鏈線程

不過 PowerShell 版仍是單線程順序下載,另外一個 C#/.NET Core 版 https://gitee.com/codefelix/spany-down-sharp 採用異步下載,有進度顯示,效率更高
rest

相關文章
相關標籤/搜索