簡體   English   中英

從瀏覽器復制所有文本,並將其粘貼到txt文件中並保存。 VB腳本

[英]Copy all texts from browser and paste it to a txt file and save it. VBscript

我有30個不同的網頁,我試圖制作一個腳本來復制所有文本並將其粘貼到30個不同的txt文件中並保存-全部都在后台。

到目前為止,我已經成功地為一個網頁創建了一個腳本,但是在創建一個.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總是向我顯示此彈出消息-“是否要允許此網頁訪問剪貼板?” 如何刪除該彈出窗口? 刪除此彈出窗口!

您使用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組件。 如果未發布,則最終會將許多作為后台進程。

剛才提到了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()
}
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

這列出了來自Explorer和Internet Explorer的所有打開的外殼窗口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM