Power Shell Web Scraping SSL/TSL问题

Sam*_*ows 5 html powershell web-scraping

我想在服务器上运行Web抓取脚本.

当前脚本收集指定页面上的html.

$url = "http://websms"
 [net.httpWebRequest] $request = [net.webRequest]::create($url)
 [net.httpWebResponse] $response = $request.getResponse()
 $responseStream = $response.getResponseStream()
 $sr = new-object IO.StreamReader($responseStream)
 $result = $sr.ReadToEnd()

 $result
Run Code Online (Sandbox Code Playgroud)

这在典型的网页上工作正常.但是我想在服务器管理页面上运行它,当然这需要登录.

我想在尝试登录之前我会尝试抓取服务器的登录页面.运行上面的脚本我得到以下结果.

   Exception calling "GetResponse" with "0" argument(s): "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."
At C:\temp\web3.ps1:3 char:56
+  [net.httpWebResponse] $response = $request.getResponse <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
Run Code Online (Sandbox Code Playgroud)

任何想法如何解决这个问题,或者如果你可以指出我一个不同的方向,所以我可以从服务器的管理html页面刮取元素.

多谢你们!

And*_*ndi 23

这一个班轮将忽略SSL证书错误:

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
Run Code Online (Sandbox Code Playgroud)

执行此操作后,将忽略有关自签名不受信任证书,名称不匹配或过期的错误.

  • 要恢复,只需执行`[System.Net.ServicePointManager] :: ServerCertificateValidationCallback = $ null` (7认同)