【问题标题】:MSXML2.XMLHTTP Request does not return full HTML pageMSXML2.XMLHTTP 请求不返回完整的 HTML 页面
【发布时间】: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

【问题讨论】:

    标签: html excel vba


    【解决方案1】:

    如果您查看生成的文本文件,您会发现&lt;span id="orderBuyTq" class="bold"&gt;-&lt;/span&gt;,所以实际内容是-,并且您的代码可以正常工作。

    如果我在浏览器中打开您的 URL https://www.nseindia.com/get-quotes/equity?symbol=VOLTAS 并查看 HTML 代码以搜索 orderBuyTq,也会发生同样的情况。它的内容是-

    所以你的代码完全按照它应该做的。

    问题是该值是由一些 JavaScript 加载的,因为当您执行 MSXML2.XMLHTTP 请求时它不运行任何 JavaScript,该值不存在。您可以通过在浏览器中禁用 JavaScript 来测试它。

    我建议改用以下网址
    https://www.nseindia.com/api/quote-equity?symbol=VOLTAS&amp;section=trade_info
    直接访问 API,因此您根本不必摆弄 HTML。它为您提供以下 JSON 响应,然后您在 "marketDeptOrderBook" 中拥有 "totalBuyQuantity":355"totalSellQuantity":0

    {
       "noBlockDeals":true,
       "bulkBlockDeals":[
          {
             "name":"Session I"
          },
          {
             "name":"Session II"
          }
       ],
       "marketDeptOrderBook":{
          "totalBuyQuantity":355,
          "totalSellQuantity":0,
          "bid":[
             {
                "price":1048.7,
                "quantity":355
             },
             {
                "price":0,
                "quantity":0
             },
             {
                "price":0,
                "quantity":0
             },
             {
                "price":0,
                "quantity":0
             },
             {
                "price":0,
                "quantity":0
             }
          ],
          "ask":[
             {
                "price":0,
                "quantity":0
             },
             {
                "price":0,
                "quantity":0
             },
             {
                "price":0,
                "quantity":0
             },
             {
                "price":0,
                "quantity":0
             },
             {
                "price":0,
                "quantity":0
             }
          ],
          "tradeInfo":{
             "totalTradedVolume":3011002,
             "totalTradedValue":31321.35,
             "totalMarketCap":3475778.75,
             "ffmc":2428991.787866,
             "impactCost":0.03
          },
          "valueAtRisk":{
             "securityVar":14.13,
             "indexVar":0,
             "varMargin":14.13,
             "extremeLossMargin":3.5,
             "adhocMargin":0,
             "applicableMargin":17.63
          }
       },
       "securityWiseDP":{
          "quantityTraded":3011002,
          "deliveryQuantity":720105,
          "deliveryToTradedQuantity":23.92,
          "seriesRemarks":null,
          "secWiseDelPosDate":"25-FEB-2021 EOD"
       }
    }
    

    确保您在自动处理该网站时没有违反该网站的条款。通常使用条款不允许使用自动脚本。

    【讨论】:

    • 我使用了你直接提到的 url,使用 json 解析器我收到了响应文本,它不包含任何数据。可能是 cookie 问题吗?你能稍微说明一下你的方法或代码吗?
    • @saravana 是的,看起来他们期待 cookie。所以很明显他们不希望有人窃取他们的数据。从您在问题中发布的 URL 中找出您需要的 cookie,然后查看 this 如何使用 cookie。
    猜你喜欢
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-24
    • 2019-03-12
    • 2019-01-29
    • 1970-01-01
    相关资源
    最近更新 更多