Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

vbscript - vbs to download data from url as csv table

I am using the following code to download data from a website using vbs. The data is held at the url in table form. However, the resulting downloaded data is in the form of a simple continuous text data.

The solution at https://www.example-code.com/vbscript/html_table_to_csv.asp allows converting the downloaded data to csv format, but requires specific api and software to be pre-installed.

I was wondering if it would be possible to download/ convert the data in csv format using vbs only and without using a third-party software. Perhaps above link could give some ideas?

(I can download the same using excel etc., but I find vbs to be much faster and efficient).

Note:

  1. the file needs to be saved in D: as any_name.vbs and resulting downloaded data file will be downloaded in D:
For i = 1 to 1
createFile(i)
Next

Public Sub createFile(a)

    Dim fso,MyFile
    filePath = "D:file_name" & a & ".txt"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set MyFile = fso.CreateTextFile(filePath)


myURL = "https://example-code.com/data/etf_table.html"

'Create XMLHTTP Object & HTML File
Set oXMLHttp = CreateObject("MSXML2.XMLHTTP")
Set ohtmlFile = CreateObject("htmlfile")

'Send Request To Web Server
oXMLHttp.Open "GET", myURL, False
oXMLHttp.send

'If Return Status is Success
If oXMLHttp.Status = 200 Then

    'Get Web Data to HTML file Object
    ohtmlFile.Write oXMLHttp.responseText
    ohtmlFile.Close
        
    'Parse HTML File
    Set oTable = ohtmlFile.getElementsByTagName("table")
    For Each oTab In oTable
        MyFile.WriteLine oTab.Innertext
    Next
        MyFile.close
End If

End Sub

'Process Completed
'WScript.Quit
question from:https://stackoverflow.com/questions/65873401/vbs-to-download-data-from-url-as-csv-table

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would propose this solution:

For i = 1 to 1
createFile(i)
Next

Public Sub createFile(a)

    Dim fso,MyFile
    filePath = "z:\_ComunityStackOverflowfile_name" & a & ".txt"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set MyFile = fso.CreateTextFile(filePath)


myURL = "https://example-code.com/data/etf_table.html"

'Create XMLHTTP Object & HTML File
Set oXMLHttp = CreateObject("MSXML2.XMLHTTP")
Set ohtmlFile = CreateObject("htmlfile")

'Send Request To Web Server
oXMLHttp.Open "GET", myURL, False
oXMLHttp.send

'If Return Status is Success
If oXMLHttp.Status = 200 Then

    'Get Web Data to HTML file Object
    ohtmlFile.Write oXMLHttp.responseText
    ohtmlFile.Close
        
   'Parse HTML File
    Set oTable_coll = ohtmlFile.getElementsByTagName("table")
    For Each oTab_enum In oTable_coll
        For Each oRow_enum In oTab_enum.rows
            ROW = ""
            For Each oCell_enum In oRow_enum.cells
                ROW = ROW & oCell_enum.innerText & ","
            Next
            ROW = Left(ROW, Len(ROW) - 1)
            CSV = CSV & ROW & vbCrLf
        Next
    Next
    MyFile.WriteLine CSV
    MyFile.close
End If

End Sub

'Process Completed
'WScript.Quit

REMARK: TESTED WORKS !!! btw. and I'm not well VBS scripter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...