如何使用 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** 文件夹中。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP