2017-04-19 33 views
0

我想保存一個網頁(ASP),就像IE將它保存爲文本文件一樣。PowerShell保存IE網頁文本

我發現this和嘗試:

$ie=New-Object -ComObject InternetExplorer.Application 
$ie.Navigate($Page) 
$ie.Document.execCommand("SaveAs", $false, "1.txt") 

但我得到的錯誤:

You cannot call a method on a null-valued expression. 
At line:3 char:1 
+ $ie.Document.execCommand("SaveAs", $false, "1.txt") 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (:) [], RuntimeException 
    + FullyQualifiedErrorId : InvokeMethodOnNull 

我不知道我在做什麼錯。

如果我嘗試通過

$web = New-Object Net.WebClient 
$web | Get-Member 
$web.DownloadString($Page) | Out-File "test.html" 

我得到一個頁面捕捉頁面,但它是由網站讓我知道該鏈接是無效的創建了一個錯誤頁面,所以它實際上只如果我發現頁面通過IE會話加載它。

我真的只需要自動捕獲IE頁面作爲文本文件,我希望不使用SendKey函數。任何人都可以協助

+0

也許你不等待IE來完成加載頁面和解析HTML並生成一個DOM,所以沒有.Document呢。您通常需要'.Navigate(..)'然後'while($ ie.Busy - 或$ ie.ReadyState -ne 4){start-sleep -Milliseconds 100}'等待它加載內容,之前用它做任何事情。 – TessellatingHeckler

+0

謝謝。也許就是這樣,但現在它正在運行/掛起。不應該只有1/10秒的延遲嗎? – Nate

+0

@TessellatingHeckler我也在導航和execCommand之間嘗試了'Start-Sleep 10',但得到了同樣的錯誤。添加你建議的線路仍然會永遠掛起。想法? – Nate

回答

0

嘗試調用-的WebRequest:

Invoke-WebRequest -Uri "http://www.powershell.org" -OutFile "powershell.txt"