【发布时间】:2021-02-25 10:58:00
【问题描述】:
我想仅使用 XMLHTTP 从 NSE 网站提取买入和卖出数量数据,因为它可以非常快速地提取数据(Set ie = CreateObject("InternetExplorer.Application") 的常规方法)需要很长时间才能提取 100 多只股票的数据。
最初我尝试使用“MSXML2.XMLHTTP”方法,我无法提取数据,因为收到的响应文本没有。后来看了几次故障排除后,我从这个网站本身找到了一些解决方案,首先将HTML文档写入文本文件然后尝试提取。
使用该方法后,对此进行了一些改进,除了我的必填字段之外,收到的响应文本包含所有 HTML 元素。我能够看到我需要的 getdocumentbyid 但我无法获取其内部文本。收到的内部文本为"-"。
有人可以帮忙吗?
在此处附上我的代码
Public Sub GetInfo()
Dim sResponse As String, I As Long, html As New HTMLDocument, hTable As HTMLTable
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://www.nseindia.com/get-quotes/equity?symbol=VOLTAS", False
.send
Application.Wait Now + TimeValue("00:00:01")
sResponse = StrConv(.responseBody, vbUnicode)
End With
sResponse = Mid$(sResponse, InStr(1, sResponse, "<!DOCTYPE "))
WriteTxtFile sResponse
With html
.body.innerHTML = sResponse
Set hTable = .getElementById("orderBuyTq")
Debug.Print hTable.innerHTML 'It gives output as "-"
End With
End Sub
Public Sub WriteTxtFile(ByVal aString As String, Optional ByVal filePath As String = "E:\Share Market\Test.txt")
Dim fso As Object, Fileout As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set Fileout = fso.CreateTextFile(filePath, True, True)
Fileout.Write aString
Fileout.Close
End Sub
【问题讨论】: