2016-07-27 82 views
0

我有30個不同的網頁,我正在試圖製作一個腳本來複制所有文本,並將其粘貼到30個不同的txt文件並保存 - 全部在後臺。從瀏覽器複製所有文本並將其粘貼到txt文件並保存。 VBscript

到目前爲止,我成功地爲一個網頁創建了一個腳本,但是我有問題要創建一個.vbs文件,它將爲所有30個頁面創建。我以爲我可以將我的代碼30x複製/粘貼到頁面底部,只需修改網站的源/目的地即可。但它沒有。

With CreateObject("internetexplorer.application") 
    .Navigate "http://example.com" 
    Do Until .ReadyState = 4 
    Wscript.Sleep 100 
    Loop 

    .Document.execcommand "SelectAll" 
    .Document.execCommand "copy" 

End With 

'paste 
    Const ForAppending = 8 

    Dim sFSpec 
    sFSpec = ".\file1.txt" 

    Dim oIE 
    Dim sText 

    Set oIE = CreateObject("InternetExplorer.Application") 
    oIE.Navigate("about:blank") 
    sText = oIE.document.parentwindow.clipboardData.GetData("text") 
    CreateObject("Scripting.FileSystemObject")_ 
    .OpenTextFile(sFSpec, ForAppending, True)_ 
    .WriteLine sText 

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
    'Below, I just copy and paste it, but the code here doesn't work 

    With CreateObject("internetexplorer.application") 
    .Navigate "http://example1.com" 

    Do Until .ReadyState = 4 
    Wscript.Sleep 100 
    Loop 
    .Document.execcommand "SelectAll" 
    .Document.execCommand "copy" 

End With 

'paste 
    Const ForAppending = 8 
    Dim sFSpec1 

    sFSpec1 = ".\dev01-envVar.txt" 

    Dim oIE1 
    Dim sText1 

    Set oIE1 = CreateObject("InternetExplorer.Application") 
    oIE1.Navigate("about:blank") 
    sText1 = oIE1.document.parentwindow.clipboardData.GetData("text") 
    CreateObject("Scripting.FileSystemObject")_ 
    .OpenTextFile(sFSpec, ForAppending, True)_ 
    .WriteLine sText 

或者比使用vbscripting更簡單的方法嗎?

此外,IE總是給我這個彈出消息 - 「你想讓這個網頁訪問你的剪貼板嗎?」我怎樣才能刪除彈出? Remove this popup!

+0

此聲明 - *「或者使用任何其他比使用vbscripting更簡單的方法?」*使問題過於寬泛。參見[問]和[我應該避免詢問什麼類型的問題?](http://stackoverflow.com/help/dont-ask)。 – Lankymart

+0

爲什麼當[[WinHttpRequest]對象]使用'InternetExplorer.Application'的實例(https://msdn.microsoft.com/zh-cn/library/windows/desktop/aa384106%28v=vs.85%29 .aspx?f = 255&MSPPError = -2147217396)在沒有開銷的情況下也能正常工作? – Lankymart

回答

0

你打上標籤的PowerShell你的問題所以在這裏,它是:

"http://example.com", "http://gmail.com" | % { 
    $ie = New-Object -ComObject internetexplorer.application 
    $ie.Navigate($_) 
    while ($ie.ReadyState -ne 4){ 
     Start-Sleep -Milliseconds 100 
    } 
    $ie.Document.execCommand("SelectAll") | Out-Null 
    Out-File -InputObject $ie.Document.selection.createRange().text ` 
      -FilePath "D:\Temp\$($ie.Document.title).txt" 
    $ie.Quit() 
} 
  1. 我略有改變這個例子。它不需要剪貼板訪問並且不會污染這個區域(你可能在剪貼板中有一些有價值的東西),不需要改變剪貼板訪問權限,安全性得到改善。
  2. 文件以頁面標題命名,您可以更改它。
  3. 不要忘記處理IE組件。如果它沒有被釋放,你最終會將它們當中的許多作爲後臺進程。
+0

他們發佈的代碼非常清楚VBScript,但僅僅是因爲他們錯誤地標記了它[標籤:powershell],你認爲你會進一步混淆這個問題。 – Lankymart

+0

如果你打算使用powershell,爲什麼不提示['System.Net.WebClient'](http://stackoverflow.com/a/509394/692942)? – Lankymart

+0

簡單:因爲只需要文本。沒有標籤,沒有樣式信息,沒有腳本。解析HTML並不容易。 –

0

Sinse你提到的PowerShell標籤:

編輯在PowerShell中V2.0

Add-Type -AssemblyName system.windows.forms 
[System.Windows.Forms.Clipboard]::Clear() # just to be sure 

$sitesList = @(
    'http://example.com', 
    <# 
     More sites here 
    #> 
    'http://www.example.com' 
) 

foreach ($site in $sitesList) { 
    $ie = New-Object -ComObject "internetexplorer.application" 
    $ie.Navigate($site) 

    while ($ie.ReadyState -ne 4) { 
     Start-Sleep -Milliseconds 100 
    } 

    $ie.Document.execCommand("SelectAll") 
    $ie.Document.execCommand("copy") 

    $filename = ($site -replace "^http://") + '.txt' 


    [System.Windows.Forms.Clipboard]::GetText() | Out-File "D:\$filename" -Force 
    [System.Windows.Forms.Clipboard]::Clear() 

    $ie.Quit() 
} 
+0

嗨,我複製了確切的代碼,但將出文件位置更改爲C:,並且它不工作....實際上是第3行中有一個錯誤......「在''後缺少表達式 在行:3 char:29「我刪除了逗號,它只能複製到剪貼板,並不粘貼到文本文件中。 – EpicDeveloper

+0

只需在第三行刪除',':) – n01d

+0

嗨n01d,感謝您的超級快速響應....但是它只能複製到我的剪貼板,而不是粘貼/保存到文本文件。 – EpicDeveloper

0
Set objShell = CreateObject("Shell.Application") 
Set AllWindows = objShell.Windows 
For Each window in AllWindows 
    msgbox window.locationname 
    If window.locationname="Scripts" then window.quit 
Next 

這將列出所有打開的shell窗口從資源管理器和Internet Explorer的工作。

+0

驚訝你鼓勵使用'InternetExplorer.Application',不會期望你至少提出一個xhr方法。 – Lankymart

+0

這不會在瀏覽器*中回答他*的問題。他的方法有缺陷,但這不是他的問題。 – 2016-07-27 09:45:16

+0

整個問題是一團糟,不清楚,方法都是錯誤的,但你仍然留下一個答案。減少不良問題的方法@Noodles。 – Lankymart

相關問題