首页 > 解决方案 > 如何从 URL 下载所有文件?

问题描述

我对 PowerShell 很陌生,需要一个脚本来从 URL 下载所有文件: https ://opendata.dwd.de/climate_environment/CDC/observations_germany/climate/daily/kl/recent/

当我将单个文件的 URL 存储在列表中时,我已经设法下载了这些文件(参见代码)。我尝试了不同的方法来自动生成列表,但我还没有做到。

$list = get-content "D:\ListURL.txt"

foreach($url in $list)
{
    $filename =[System.IO.Path]::GetFileName($url) 
    $file =[System.IO.Path]::Combine($outputdir, $filename) 
    Invoke-WebRequest -Uri $url -OutFile $file

}

任何人都可以帮助我使用一些代码从 URL 创建列表吗?提前谢谢了。

标签: powershell

解决方案


Looking at the file list on that url, this works for me:

$outputdir = 'D:\Downloads'
$url       = 'https://opendata.dwd.de/climate_environment/CDC/observations_germany/climate/daily/kl/recent/'

# enable TLS 1.2 and TLS 1.1 protocols
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12, [Net.SecurityProtocolType]::Tls11

$WebResponse = Invoke-WebRequest -Uri $url
# get the list of links, skip the first one ("../") and download the files
$WebResponse.Links | Select-Object -ExpandProperty href -Skip 1 | ForEach-Object {
    Write-Host "Downloading file '$_'"
    $filePath = Join-Path -Path $outputdir -ChildPath $_
    $fileUrl  = '{0}/{1}' -f $url.TrimEnd('/'), $_
    Invoke-WebRequest -Uri $fileUrl -OutFile $filePath
}

推荐阅读