powershell - 如何从 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 创建列表吗?提前谢谢了。
解决方案
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
}
推荐阅读
- r - 有没有办法在同一个图中绘制不同的回归模型?
- nginx - Django - 如何添加/允许 url 参数?
- android - 清单合并失败:需要明确指定 android:exported
即使我的 AndroidManifest.xml 文件中没有服务 - reactjs - Redux : 两个非常相似的命令,一个进入 store,另一个不进入
- c# - 在 Unity 中通过按键淡化图像 UI
- wordpress - Wordpress 不返回某些管理页面(如 post.php)的内容
- php - PayU Bolt 表格不接受金额
- javascript - document.cookie 与请求对象中的 cookie 不同吗?
- javascript - 使用 JavaScript 从缓冲区渲染画布图像
- java - 带有反应式 mongodb 的 Spring Boot webflux。java.lang.NoSuchMethodException:com.acme.dao.model.Transit。
()