导航到网站,选择所有,复制并粘贴到记事本中

时间:2014-01-29 16:57:01

标签: internet-explorer vbscript web-scraping wsh

我正在尝试创建一个VB脚本来导航到一个网站,SelectAll,复制,然后将复制的数据从剪贴板保存到文本文件,但我被卡住了! :(

这是我到目前为止所得到的:

With CreateObject("InternetExplorer.Application")
    .Navigate "https://www.microsoft.com"
    Do Until .ReadyState = 4: Wscript.Sleep 100: Loop
    .Visible = true
    With .Document
        .execCommand "SelectAll"
        .execCommand "Copy"
    End With ' Document

1 个答案:

答案 0 :(得分:3)

您可以尝试直接从DOM获取文本数据

With CreateObject("InternetExplorer.Application")
    .Visible = True
    .Navigate "https://www.microsoft.com"
    Do Until .ReadyState = 4
        Wscript.Sleep 100
    Loop
    For Each Tag In .Document.GetElementsByTagName("script")
        Tag.OuterHtml = ""
    Next
    For Each Tag In .Document.GetElementsByTagName("noscript")
        Tag.OuterHtml = ""
    Next
    Content = .Document.GetElementsByTagName("body")(0).InnerText
    Do While InStr(Content, vbCrLf & vbCrLf)
        Content = Replace(Content, vbCrLf & vbCrLf, vbCrLf)
    Loop
    ShowInNotepad Content
    .Quit
End With

Sub ShowInNotepad(Content)
    With CreateObject("Scripting.FileSystemObject")
        TempPath = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%TEMP%") & "\" & .GetTempName
        With .CreateTextFile(TempPath, True, True)
            .WriteLine (Content)
            .Close
        End With
        CreateObject("WScript.Shell").Run "notepad.exe " & TempPath, 1, True
        .DeleteFile (TempPath)
    End With
End Sub