当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell wget | PowerShell PowerShell wget 如何与示例一起使用?

作者:精品下载站 日期:2024-12-14 20:33:31 浏览:15 分类:玩电脑

PowerShell wget | PowerShell PowerShell wget 如何与示例一起使用?


[玩转系统] PowerShell wget | PowerShell PowerShell wget 如何与示例一起使用?

PowerShell wget 简介

PowerShell Wget 是 PowerShell 中 Invoke-WebRequest 的别名,是一个非交互式实用程序,它将请求发送到 HTTP 或 HTTPS 网页或 Web 服务,解析响应并返回链接、图像的集合,和HTML元素,它还有助于从网页下载文件,用表单发布或删除或修改网站上的数据,检查网站的状态等。

语法

Invoke-WebRequest
[-UseBasicParsing]
[-Uri] <Uri>
[-WebSession <WebRequestSession>]
[-SessionVariable <String>]
[-AllowUnencryptedAuthentication]
[-Authentication <WebAuthenticationType>]
[-Credential <PSCredential>]
[-UseDefaultCredentials]
[-CertificateThumbprint <String>]
[-Certificate <X509Certificate>]
[-SkipCertificateCheck]
[-SslProtocol <WebSslProtocol>]
[-Token <SecureString>]
[-UserAgent <String>]
[-DisableKeepAlive]
[-TimeoutSec <Int32>]
[-Headers <IDictionary>]
[-MaximumRedirection <Int32>]
[-MaximumRetryCount <Int32>]
[-RetryIntervalSec <Int32>]
[-Method <WebRequestMethod>]
[-Proxy <Uri>]
[-ProxyCredential <PSCredential>]
[-ProxyUseDefaultCredentials]
[-Body <Object>]
[-Form <IDictionary>]
[-ContentType <String>]
[-TransferEncoding <String>]
[-InFile <String>]
[-OutFile <String>]
[-PassThru]
[-Resume]
[-SkipHttpErrorCheck]
[-PreserveAuthorizationOnRedirect]
[-SkipHeaderValidation]
[<CommonParameters>]

如果我们检查 Invoke-Webrequest 语法,PowerShell 7.1 版本支持该命令的 4 组。

其他 3 组包括以下额外参数。

-无代理

-自定义方法

这意味着您不能将上述 2 个参数与第一组某些参数组合起来。例如,您不能同时使用 -Proxy 和 -NoProxy 参数,但同时设置支持 -NoProxy 和 -CustomMethod 。

PowerShell wget 如何工作?

Wget 实用程序是基于 Unix 的实用程序,通常人们用它从网页下载文件。 PowerShell以cmdlet Invoke-WebRequest的形式引入了类似的实用程序,其别名为wget,以便来自Unix背景的人可以理解该命令具有PowerShell中类似或更高级的功能。

Invoke-WebRequest 在 PowerShell 3.0 中引入,并且在与网页交互方面变得非常流行。

Wget 是 PowerShell .Net 框架版本(v1.0 至 5.1)中 Invoke-WebRequest 命令的别名。

Get-Alias -Name wget

输出:

[玩转系统] PowerShell wget | PowerShell PowerShell wget 如何与示例一起使用?

在 Powershell Core 版本(6.0 及以上)中,wget 别名被替换为 iwr 命令。

Get-Alias -Definition Invoke-Webrequest

输出:

[玩转系统] PowerShell wget | PowerShell PowerShell wget 如何与示例一起使用?

Wget 和 iwr 还具有相同的受支持实用程序,称为“curl”,它是一个 Unix 命令,但作为 Invoke-Webequest 命令的别名引入。

Get-Alias -Definition Invoke-WebRequest

输出:

[玩转系统] PowerShell wget | PowerShell PowerShell wget 如何与示例一起使用?

当您使用 Wget 命令解析网页时,一些属性和方法与该命令相关联。让我们看看这些成员是什么。

$uri = "https://theautomationcode.com"
$response = wget -Uri $uri
$response | gm

输出:

[玩转系统] PowerShell wget | PowerShell PowerShell wget 如何与示例一起使用?

有各种属性,如标题、图像、链接,您可以直接通过 wget 命令检索。

在下面的示例中,我们将了解 wget 命令如何支持各种参数。

PowerShell wget 示例

下面给出了 PowerShell wget 的示例:

例子#1

使用wget命令查看网站状态

我们可以使用wget(Invoke-WebRequest)命令检查网页的状态。

$uri = "https://theautomationcode.com"
wget -Uri $uri

输出:

[玩转系统] PowerShell wget | PowerShell PowerShell wget 如何与示例一起使用?

StatusCode 200 表示该站点正常。您可以在下面的 wiki 页面链接上查看各种状态代码。

https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

例子#2

Wget 命令中的异常处理。

编程语言使用 try/catch 方法进行异常处理。在本例中,我们将在PowerShell中使用相同的方法处理不存在网站异常。

Try {
$uri = "https://theautomationcode222.com"
wget -Uri $uri -EA Stop
}
Catch {
Write-Host $($_.Exception.Message) -BackgroundColor DarkRed
}

输出:

[玩转系统] PowerShell wget | PowerShell PowerShell wget 如何与示例一起使用?

捕获消息的状态代码。

try{
$uri = "www.microsoft.com/unkownhost"
wget -Uri $uri -EA Stop
}
catch{
Write-Output "Status Code:  $($_.Exception.Response.StatusCode.value__)"
}

输出:

[玩转系统] PowerShell wget | PowerShell PowerShell wget 如何与示例一起使用?

例子#3

使用 Wget 命令下载文件。

我们可以使用 wget 命令直接从互联网下载文件,如下所示。本例中我们从 MS 网站下载一个基于 Web 的网络框架,如下所示。

$uri = https://go.microsoft.com/fwlink/?LinkId=2085155
wget -Uri $uri -OutFile "C:\temp\netframework.exe" -Verbose

输出:

[玩转系统] PowerShell wget | PowerShell PowerShell wget 如何与示例一起使用?

上述命令会将下载文件保留在 C:\temp 文件夹中,名称为 NetFramework.exe。

例子#4

从网页下载图像、链接。

通过wget小部件,我们可以直接从网站访问本地系统的图像或链接,如下所示。

$uri = "https://theautomationcode.com"
$response = wget -Uri $uri

使用上述命令,来自网页的响应将存储在 $Response 变量中。因此,我们可以直接访问这些属性。但是,首先,让我们检查一下网站上存储的图像。

$response.Images.src

输出:

[玩转系统] PowerShell wget | PowerShell PowerShell wget 如何与示例一起使用?

上面的命令显示了网站图像的来源。

通过对上述命令进行一些操作,您可以将这些图像下载到特定文件夹,如下所示。它将把链接中的所有图像下载到 C:\temp\WebImages 文件夹。

foreach($img in $response.Images.src){
$imgurl = ($img.Split('?'))[0]
$imgname = ($imgurl -split '/')[-1]
wget -Uri $imgurl -OutFile "C:\temp\WebImages$imgname" -Verbose
}

输出:

[玩转系统] PowerShell wget | PowerShell PowerShell wget 如何与示例一起使用?

同样,您可以访问网站上的链接。

$response.Links.Href

例子#5

使用 wget 将 Rest API 内容转换为 JSON 数据。

当我们使用 REST API 获取网站数据时,该数据通常采用 JavaScript 对象表示法 (JSON) 格式。因此,当我们使用Invoke-RestMethod命令时,它直接将输出转换为JSON格式,如下所示。

Invoke-RestMethod -Uri "https://jsonplaceholder.typicode.com/posts"

输出:

[玩转系统] PowerShell wget | PowerShell PowerShell wget 如何与示例一起使用?

但是使用wget命令,我们需要使用一些cmdlet来过滤内容,然后才能转换为上面的输出显示格式。

$uri = 'https://jsonplaceholder.typicode.com/posts'
$response = Invoke-WebRequest -Uri $uri
$response.Content | ConvertFrom-Json

您将得到与第一个相同的输出。

例子#6

使用wget post方法登录网站。

在下面的示例中,我们将使用 LinkedIn 网站使用 wget 表单登录,如下所示。

$linloginpage = 'https://www.linkedin.com/login?fromSignIn=true&trk=guest_homepage-basic_nav-header-signin'
$response = wget -Uri $linloginpage -SessionVariable LIN
$form = $response.Forms[1]
$form.Fields["Username"] = "user@emailid"
$form.Fields["Password"] = "Your password"
$uri = "https://www.linkedin.com" + $form.Action
$r = wget -Uri $uri -WebSession $Lin -Method Post -Body $form.Fields
Write-Output "`n`nWebsite Status: $($r.StatusDescription)"

输出:

[玩转系统] PowerShell wget | PowerShell PowerShell wget 如何与示例一起使用?

结论

Wget 或 Invoke-WebRequest(Curl、iwr)是网页抓取的最佳命令行工具之一,被各种开发人员和脚本编写人员用来调用 API,传递、删除、修改、添加信息到 API 或网页无需使用GUI,也有助于处理各种类型或网页错误和报告目的。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯