2015-06-30 139 views
2

我有一個列表,其中包含大約100多個必須分析的文本文件的網頁。複製和粘貼可能很忙碌。從文本文件讀取域並在瀏覽器中打開

我認爲在一次打開一個域的腳本中運行一個文本文件是有效的,在我關閉IE之後,它會打開下一個域。

如果可能,更新列表並刪除被訪問的列表,但如果有很多工作,我可以傳遞此信息。

假設有:

yahoo.com 
google.com 
microsoft.com 

PowerShell的它並沒有真正的工作,因爲我剛剛潛入PowerSshell。

$domain = Get-Content ".\list.txt" 

ForEach($element in $domain){ 
    $url = "$domain" 
    $ie = new-object -com "InternetExplorer.Application" 
    $ie.Navigate($url) 
} 

的VBScript 這 作品,但一旦打開的所有網頁。如果有很多網址,可能會崩潰。我如何控制它?

Set fso = CreateObject("Scripting.FileSystemObject") 
Set listFile = fso.OpenTextFile("list.txt") 
Set WshShell = WScript.CreateObject("WScript.Shell") 
Dim fso 
do while not listFile.AtEndOfStream fName = listFile.ReadLine() 

Return = WshShell.Run("iexplore.exe " & fName, 1) 
loop 

回答

1

注:更新以包括行刪除已訪問過的網址,並把IE窗口前。如果你想停止腳本它完成之前,去PowerShell窗口並關閉它或按Ctrl + C

#VB assembly needed for function to bring IE to front 
Add-Type -Assembly "Microsoft.VisualBasic" 
$domain = Get-Content "c:\temp\list.txt" 

ForEach($url in $domain){ 

    #Start IE and make it visible 
    $ie = new-object -com "InternetExplorer.Application" 
    $ie.Visible = $true 

    #Bring the IE window to the front 
    $ieProc = Get-Process | ? { $_.MainWindowHandle -eq $ie.HWND } 
    [Microsoft.VisualBasic.Interaction]::AppActivate($ieProc.Id) 

    #Navigate to the URL 
    $ie.Navigate($url) 

    #Sleep while IE is running 
    while($ie.visible){ 
     start-sleep -s 1 
     } 

     #Delete the URL from the file 
     (type c:\temp\list.txt) -notmatch "^$url$" | out-file c:\temp\list.txt 
} 

這將PowerShell中做到這一點。

+0

謝謝,它的工作。 但是,當我關閉瀏覽器時,它會拋出以下錯誤。 (異常來自HRESULT:0x80010108(RPC_E_DISCONNECTED))「 行:8 char:11 + while($ ie.Visible = $ true){ + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) [],SetValueInvocationException + FullyQualifiedErrorId:ExceptionWhenSetting' – Imsa

+0

另外,假設我運行腳本包含100個網址的文本文件,但是我想停止打開該網站,在X網址之後我該怎麼做?截至目前,它一直在打開URL直到EOF。 – Imsa

+0

請參閱編輯答案 - 新while語句將避免這種情況。另外,我的評論提到第一個版本會拋出異常。 –

1

您的VBScript失敗,因爲您沒有正確使用.Run method。第三個參數bWaitOnReturn必須設置爲True。如在

Const csFSpec = "31144615.txt" 
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject") 
Dim goWS : Set goWS = CreateObject("WScript.Shell") 
Dim tsIn : Set tsIn = goFS.OpenTextFile(csFSpec) 
Do Until tsIn.AtEndOfStream 
    Dim sLine : sLine = tsIn.ReadLine 
    goWS.Run """C:\Program Files\Internet Explorer\IEXPLORE.EXE"" """ & sLine & """", 1, True 
Loop 
tsIn.Close 
相關問題