Search code examples
pythonpython-3.xexcelvba

Getting website metadata (Excel VBA/Python)


I am trying to get information of stock code from below website using excel vba https://www.screener.in/company/KRISHANA/

I went through network tab and found out the code was in under peers node https://www.screener.in/api/company/13611040/peers/

below is the code which I've written in excel vba, the html page has no information on the code

Note: I dont want to duplicate this thread, I tried searching everywhere, if its not possible in vba, can we achieve this in python.

URLS = "https://www.screener.in/company/KRISHANA/" 
Set xhr = New MSXML2.ServerXMLHTTP60
    With xhr
        .Open "GET", URLS, False
        .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
        .send
            If .readyState = 4 And .Status = 200 Then
                Set Doc = New MSHTML.HTMLDocument
                Doc.body.innerHTML = .responseText
            End If
        End With
        DoEvents

Solution

  • If the url is always the same and you only need to change the warehouse id, you can get the id in the following way. This is without intercepting any errors and only shows the basic mechanism.

    Sub getDataWarehouseId()
    
        Const url As String = "https://www.screener.in/company/KRISHANA/"
        Dim doc As Object
        
        Set doc = CreateObject("htmlFile")
        
        With CreateObject("MSXML2.XMLHTTP.6.0")
            .Open "GET", url, False
            .send
            
            If .Status = 200 Then
                doc.body.innerHTML = .responseText
                MsgBox doc.getElementByID("company-info").getAttribute("data-warehouse-id")
            Else
                MsgBox "Page not loaded. HTTP status " & .Status
            End If
        End With
    End Sub