Download files from page (listed in source)

Scenario

You scroll a webpage an it loads files step by step and you want to download them. You check the source code of that page and the files are listed there.


Download

The following PowerShell script shows how to download the files based on the source file.

function Search-Items {
    param ([string]$Text, [string]$Pattern)

    $Text | Select-String $Pattern -AllMatches | Foreach-Object {
        $Items += @($_.Matches.Value)
    }

    return $Items
}

function Download-Items {
    param ([Parameter(ValueFromPipeline)] [string[]]$Items)

    process {
        foreach ($Item in $Items) {
            Write-Output $Item
            $File = $Item.Substring($Item.LastIndexOf("/") + 1)
            Invoke-WebRequest $Item -OutFile $File
        }
    }
}

$ProgressPreference = "SilentlyContinue" # Suppresses progress bar

$Url = "https://website.com/path/to/page"
$Request = Invoke-WebRequest $Url
$Pattern = "https://website.com/[^,]*`.jpg"

Search-Items $Request.Content $Pattern | Select-Object -Unique | Download-Items