Загрузка данных


[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
Add-Type -AssemblyName System.Web

# =====================================================================
# СПИСОК ВАШИХ ССЫЛОК (добавляй сколько нужно через запятую)
# =====================================================================
$allTargetUrls = @(
    "https://main.atb.su/normdocs/Lists/Docs/ByCategory.aspx?C=6...",
    "https://main.atb.su/normdocs/Lists/Docs/ByCategory.aspx?C=7...",
    "https://main.atb.su/normdocs/Lists/Docs/ByCategory.aspx?C=8..."
)

$dir = "Downloads_Bank_Docs"
# =====================================================================

if (!(Test-Path $dir)) { New-Item -ItemType Directory -Path $dir | Out-Null }
$basePath = "https://main.atb.su/normdocs/Lists/Docs/"

Write-Host "`n=== ЗАПУСК МАССОВОЙ ВЫГРУЗКИ ===" -ForegroundColor Cyan

foreach ($url in $allTargetUrls) {
    Write-Host "`n[!] Обрабатываю страницу: $($url.Substring(0, 40))..." -ForegroundColor Yellow
    
    try {
        # Загружаем страницу
        $page = Invoke-WebRequest -Uri $url -UseDefaultCredentials -TimeoutSec 30 -UseBasicParsing
        $rows = [regex]::Matches($page.Content, '(?is)<tr[^>]*>(.*?)</tr>')
        
        $countOnPage = 0
        foreach ($row in $rows) {
            $html = $row.Groups[1].Value
            
            # Проверяем статус "Действителен"
            if ($html -match "Действителен") {
                if ($html -match 'href="([^"]+?\.(pdf|docx?|xlsx?|rtf))"') {
                    $link = [System.Net.WebUtility]::HtmlDecode($matches[1])
                    
                    # Формируем URL
                    if ($link.StartsWith("http")) { $fullUrl = $link }
                    elseif ($link.StartsWith("/")) { $fullUrl = "https://main.atb.su" + $link }
                    else { $fullUrl = $basePath + $link }
                    
                    $fileName = [System.Net.WebUtility]::UrlDecode($fullUrl.Split('/')[-1])
                    $fileName = $fileName -replace '[\\\/\:\*\?\"\<\>\|]', '_'
                    $path = Join-Path $dir $fileName

                    if (!(Test-Path $path)) {
                        Write-Host "  [+] Качаю: $fileName" -ForegroundColor Green
                        Invoke-WebRequest -Uri $fullUrl -OutFile $path -UseDefaultCredentials -TimeoutSec 60
                        $countOnPage++
                    }
                }
            }
        }
        Write-Host "--- Завершено на этой странице: $countOnPage документов ---" -ForegroundColor Gray
    } catch {
        Write-Host "  [!] Ошибка на странице: $($_.Exception.Message)" -ForegroundColor Red
    }
}

Write-Host "`n=== ВСЁ ГОТОВО! Проверь папку $dir ===" -ForegroundColor Cyan
Read-Host "Нажми Enter"