如何使用 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** 資料夾中。
廣告