0

我想从 NSE wesbite 拉调用和放置数据,在网站上,有 2 个下拉框可用(参考图片)。对于每个下拉更改事件,表值都会更改。 https://www.nseindia.com/option-chain

我成功地能够使用 HTML 事件更新第一个下拉字段的 HTML 表值,并且当第二个下拉索引更改时,我使用相同的“HTML 事件”更新表值,但无法完成工作,可以请帮忙吗?

在此处输入图像描述

我也给了源代码,请建议

'Option Data Pulling
Sub Options_Pull_Active_List()

    Dim Opt_Url As String
    
    Opt_Url = ActiveSheet.Range("G3").Value
     
    Dim ie As New InternetExplorer
    
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.Top = 0
    ie.Left = 0
    ie.Width = 1000
    ie.Height = 750
    ie.AddressBar = 0
    ie.StatusBar = 0
    ie.Toolbar = 0
    ie.navigate Opt_Url
    
    Do
        DoEvents
    Loop Until ie.readyState = READYSTATE_COMPLETE

    Dim doc As HTMLDocument
    
    Set doc = ie.document

    Dim objElement As HTMLObjectElement
    Dim sDD As String

    doc.Focus

    Dim k1, k2, Opto, Sana As Integer
    
    k1 = ActiveSheet.Range("AL7").Value
    k2 = ActiveSheet.Range("AL8").Value

    For Opto = k1 To k2

        ActiveSheet.Range("AK10").Value = Opto
        Sana = ActiveSheet.Range("AL10").Value

        'instrumentType
        doc.getElementById("select_symbol").Focus 'Setting Instrument Type
        Application.Wait Now + TimeValue("00:00:04")
        doc.getElementById("select_symbol").selectedIndex = Sana

        Dim objEvent
        
        Set objEvent = doc.createEvent("HTMLEvents")
        objEvent.initEvent "change", False, True
        doc.getElementById("select_symbol").dispatchEvent objEvent 'This code successfully able to updat the HTML page
        
        Application.Wait Now + TimeValue("00:00:05")
        
        doc.getElementById("expirySelect").selectedIndex = 2
        doc.getElementById("expirySelect").dispatchEvent objEvent 'This same code does not update the table values
        '-------------------------

        Application.Wait Now + TimeValue("00:00:03")
        
        Do While ie.Busy Or ie.readyState <> 4
            DoEvents
        Loop

        '------------------
        Dim hTable As Object, hBody As Object, hTR As Object, hTD As Object
        Dim tb As Object, bb As Object, tr As Object, td As Object
        Dim i As Integer

        y = 5                                    '   'Column A in Excel
        Z = 25                                   ' 'Row 1 in Excel

        Set hTable = doc.getElementsByTagName("Table")
        'Set hTable = doc.getElementById("historicalData")

        For Each tb In hTable

            Set hBody = tb.getElementsByTagName("tbody")
            
            For Each bb In hBody

                Set hTR = bb.getElementsByTagName("tr")
                
                For Each tr In hTR

                    Set hTD = tr.getElementsByTagName("td")
                    y = 5                        ' Resets back to Default Column Set in the Excel Sheet
                     
                    For i = 0 To hTD.Length
                 
                        Sheets("M1 - Option Data Pull").Cells(Z, y).Value = hTD(i).innerText
                       
                        If Trim(Sheets("M1 - Option Data Pull").Cells(Z, y).Value) = "-" Then
                            Sheets("M1 - Option Data Pull").Cells(Z, y).Value = 0
                        End If
                      
                        If i = 11 Then
                        
                            If hTD(i + 1).getAttribute("class") = "bg-yellow" And Sheets("M1 - Option Data Pull").Range("O1").Value = 0 Then
                                Sheets("M1 - Option Data Pull").Range("O1").Value = 1
                                Sheets("M1 - Option Data Pull").Range("O2").Value = hTD(i).innerText
                            End If
                        End If
                      
                        y = y + 1
                      
                    Next i
                    
                    DoEvents
                    
                    Z = Z + 1
                Next tr
                
                Exit For
            Next bb
            
            Exit For
        Next tb
        '-------------------
    Next Opto

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

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub
4

0 回答 0