How to Web Scrape the Site further?

2019-08-10 22:05发布

Following is the code which I used to fetch specific variables which are mentioned below.

Option Explicit
Public Sub GetInfo()
    Dim s As String, ids(), i As Long
ids = Array(500325, 500510)
With CreateObject("MSXML2.XMLHTTP")
        For i = LBound(ids) To UBound(ids)
            .Open "GET", "https://api.bseindia.com/BseIndiaAPI/api/ComHeader/w?quotetype=EQ&scripcode=" & ids(i) & "&seriesid=", False
            .send
            s = .responseText
            ActiveSheet.Cells(i + 1, 1) = Split(Split(s, """ROE"":""")(1), Chr$(34))(0)
            ActiveSheet.Cells(i + 1, 2) = Split(Split(s, """PE"":""")(1), Chr$(34))(0)
            ActiveSheet.Cells(i + 1, 3) = Split(Split(s, """PB"":""")(1), Chr$(34))(0)
        Next
    End With
End Sub

But I cannot further do it especially mentioned in the Results and Shareholding Pattern box as I earlier thought the code will work with all the variable mentioned on that portal. Has it to with the api or something else? As I don't know much about api, so suggest in this regard.

Link for your ready reference: https://www.bseindia.com/stock-share-price/larsen--toubro-ltd/lt/500510/

https://www.bseindia.com/stock-share-price/reliance-industries-ltd/reliance/500325/

1条回答
男人必须洒脱
2楼-- · 2019-08-10 22:15

Your array is wrong. Declare the dynamic array then assign to it.

Option Explicit

Public Sub GetInfo()
    Dim s As String, ids(), i As Long
    ids = Array(500325, 500510)
    With CreateObject("MSXML2.XMLHTTP")
        For i = LBound(ids) To UBound(ids)
            .Open "GET", "https://api.bseindia.com/BseIndiaAPI/api/ComHeader/w?quotetype=EQ&scripcode=" & ids(i) & "&seriesid=", False
            .send
            s = .responseText
            ActiveSheet.Cells(i + 1, 1) = Split(Split(s, """ROE"":""")(1), Chr$(34))(0)
            ActiveSheet.Cells(i + 1, 2) = Split(Split(s, """PE"":""")(1), Chr$(34))(0)
            ActiveSheet.Cells(i + 1, 3) = Split(Split(s, """PB"":""")(1), Chr$(34))(0)
        Next
    End With
End Sub
查看更多
登录 后发表回答