如何使用 PowerShell 中的 Invoke-Webrequest 下载图片?


要使用 Invoke-WebRequest 命令从网页下载图片,我们可以使用结果中的 images 属性来检索图片的 URL,稍后我们就可以使用该方法将它们下载到特定位置。考虑我们有要检索图片的 URI: https://theautomationcode.com

一旦你运行下面的命令,你就可以在其中看到 Images 属性。

Invoke-WebRequest -Uri "https://theautomationcode.com/feed/"

要检索图片 URL,

$req = Invoke-WebRequest -Uri "https://theautomationcode.com/feed/"
$req.Images | Select -ExpandProperty src

输出

https://i1.wp.com/theautomationcode.com/wp-content/uploads/2020/11/image-9.png?resize=178%2C60&ssl=1 https://i0.wp.com/theautomationcode.com/wp-content/uploads/2020/11/image-10.png?resize=640%2C68&ssl=1

以上所有 URL 都指向图片,所以我们可以下载它们。

$wc = New-Object System.Net.WebClient
$req = Invoke-WebRequest -Uri "https://theautomationcode.com/feed/"
$images = $req.Images | Select -ExpandProperty src
$count = 0
foreach($img in $images){    
   $wc.DownloadFile($img,"C:\Temp\WebImages\img$count.jpg")
}

输出图片将存储在 **C:\temp\WebImages** 文件夹中。

更新于: 2021 年 1 月 18 日

已被观看 3K 次

开启 职业 生涯

完成课程即可获得认证

开始
广告
© . All rights reserved.