sea*_*ull 5 windows powershell
我正在尝试抓取此网页,因为没有其他方法可以在其内容更改时自动收到警报:
https://airsdk.harman.com/runtime
使用 cURL 可以很好地下载页面(此时可以解析其内容),但使用 Invoke-WebRequest 或 System.Net.WebClient 的 DownloadFile/DownloadString 方法会导致出现错误,表示 Web 服务器返回了 404错误。
检查 Chrome 确认页面始终以 404 响应,但也返回内容,这正是我想要的。
使用 PowerShell 5.1,有没有办法指示 Invoke-WebRequest 忽略虚假的 404 错误,或者有某种方法可以让我无论如何都可以获取响应数据?
在PowerShell 7中,有一个-SkipHttpErrorCheck可以让其Invoke-WebRequest在用例中表现得像您想要的那样。
Invoke-WebRequest https://airsdk.harman.com/runtime -SkipHttpErrorCheck -OutFile C:\install\test.html
Run Code Online (Sandbox Code Playgroud)
在PowerShell 5.1中,使用curl.exe. 如果您使用的是 Windows 10 v1803 或更高版本,curl.exe则随操作系统一起提供,如果您使用的是较低版本,则需要手动下载。
curl.exe https://airsdk.harman.com/runtime --output C:\install\abc.html
Run Code Online (Sandbox Code Playgroud)
请记住指定,.exe因为curl没有它只是一个别名Invoke-WebRequest
如果您不想使用curl.exe,您所能做的就是将其包装起来try/catch并通过异常访问响应数据,但不会真正将其作为文件下载,并且没有您可能想要的那么多信息。
Try {
Invoke-WebRequest https://airsdk.harman.com/runtime -ErrorAction Stop
} Catch {
$_.Exception.Response
}
IsMutuallyAuthenticated : False
Cookies : {}
Headers : {Connection, Vary, X-Content-Type-Options, X-XSS-Protection...}
SupportsHeaders : True
ContentLength : 1123
ContentEncoding :
ContentType : text/html;charset=UTF-8
CharacterSet : UTF-8
Server :
LastModified : 08.10.2021 19:01:01
StatusCode : NotFound
StatusDescription :
ProtocolVersion : 1.1
ResponseUri : https://airsdk.harman.com/runtime
Method : GET
IsFromCache : False
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7532 次 |
| 最近记录: |