简体   繁体   中英

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

I have 30 different web pages, where I am trying to make a script to copy all the texts and paste it to 30 different txt files and save it - all in the background.

So far I successfully created one script for one web page, but I am having problems to create ONE .vbs file that will do it for all 30 pages. I thought I can just copy/paste my code 30x to the bottom of the page and just modify the source/destination of the website and it would work. But it didnt.

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 

Or any other easier way than using vbscripting?

Also, IE always gives me this pop-up message - "Do you want to allow this webpage to access your clipboard?" How can I remove that pop up? Remove this popup!

You marked your question with PowerShell tag so here it is:

"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. I changed this example slightly. It does not need clipboard access and does not pollute this area (you may have something valuable in clipboard), no need to change clipboard access permission, security is improved.
  2. Files are named after page titles, you may change it.
  3. Dont forget to dispose IE component. If it's not released, you end up having many of them as processes in background.

Sinse you mentioned PowerShell tag:

Edited to work in 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

This lists all open shell windows from Explorer and Internet Explorer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM