简体   繁体   中英

How can I save Website data to file using VBScript?

In the following code, how can I save the text to a text file (text.txt for example) instead of the current MsgBox ?

myURL = "http://URL.com"

Set oXMLHttp = CreateObject("MSXML2.XMLHTTP")
Set ohtmlFile = CreateObject("htmlfile")

oXMLHttp.Open "GET", myURL, False
oXMLHttp.send

If oXMLHttp.Status = 200 Then

ohtmlFile.Write oXMLHttp.responseText
ohtmlFile.Close

Set oTable = ohtmlFile.getElementsByTagName("table")
For Each oTab In oTable
    MsgBox oTab.Innertext
Next
End If

WScript.Quit

Please, help me!

Thanks!

You can use the FileSystemObject's OpenTextFile method .

You can create the FileSystemObject at the top of your code with your other objects:

Set objFSO = CreateObject("Scripting.FileSystemObject")

And add these constants:

Const ForReading = 1, ForWriting = 2, ForAppending = 8

If you want to append everything into the same file, you can create and open the file outside of your loop:

sFileName = "c:\text.txt"
Set objFile = objFSO.OpenTextFile(sFileName, ForAppending, True)
For Each oTab In oTable
    objFile.WriteLine oTab.Innertext
Next
objFile.Close

Otherwise you can create multiple files within your loop:

Dim iTableCounter
iTableCounter = 0

For Each oTab In oTable

    iTableCounter = iTableCounter + 1
    sFileName = "c:\table_" & iTableCounter & ".txt" ' create a dynamic file name using table name perhaps

    Set objFile = objFSO.OpenTextFile(sFileName, ForWriting, True)
    objFile.Write oTab.Innertext
    objFile.Close

Next

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