Excel VBA Web Scraping IE NAVIGATE方法相对于MSXML2.XMLHTTP60不起作用

问题描述 投票:0回答:1

我正在尝试从NSE网站上提取一个参数,网址为https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=INDUSINDBK&instrument=FUTSTK&type=-&strike=-&expiry=30APR2020

我能够使用IE.Navigate(Internet Explorer)方法(打开浏览器并获取数据)来抓取我想要的东西,但这需要很长时间,我希望快速提取结果,所以我决定与“ MSXML2.XMLHTTP60”方法一起使用,尝试时响应文本将返回内部服务器错误

下面我给出了两个代码,请帮助我在MSXML2.XMLHTTP60方法中抓取数据

Sub NSE_Data_Pull() (Working Code but takes time)

Application.EnableEvents = False
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim ie As New InternetExplorer
Set ie = CreateObject("InternetExplorer.Application")

ie.Visible = True
ie.navigate "https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=INFY&instrument=FUTSTK&type=-&strike=-&expiry=30APR2020#"
Do

DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE

Dim doc As HTMLDocument
Set doc = ie.document

doc.Focus
ActiveSheet.Cells(2, 3) = doc.getElementById("pchangeinOpenInterest").innerText


ie.Quit
ie.Visible = True
Set doc = Nothing
Set ie = Nothing


Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.EnableEvents = True

End Sub


Sub NSE_Data_Pull() (Need help here only,response text returns internal server error) -Fast Method

    Dim xhr As MSXML2.XMLHTTP60, html As MSHTML.HTMLDocument
    Set xhr = New MSXML2.XMLHTTP60
    Set html = New MSHTML.HTMLDocument

    With xhr
        .Open "GET", "https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=BEL&instrument=FUTSTK&type=-&strike=-&expiry=30APR2020#", False
        .send
         html.body.innerHTML = StrConv(.responseBody, vbUnicode)
    End With
    
    Debug.Print html.body.innerHTML
   ActiveSheet.Cells(2, 3).Value = html.getElementById("pchangeinOpenInterest").innerHTML
End Sub
html excel vba web-scraping
1个回答
0
投票

此代码对我来说运行正常。

Sub xxx()
    Dim xhr As New MSXML2.XMLHTTP60

    With xhr
        .Open "GET", "https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=BEL&instrument=FUTSTK&type=-&strike=-&expiry=30APR2020#", False
        .send
        Debug.Print StrConv(.responseBody, vbUnicode)
    End With

End Sub

可能问题不在MSXML上。那里有HTTP代理服务器吗?

© www.soinside.com 2019 - 2024. All rights reserved.