繁体   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