Cool_Pacific
New Member
- Joined
- May 26, 2017
- Messages
- 7
Hi there,
I am trying to scrape a web page, I could read most of the data present at current page for all years (Mar 16, Mar 15, Mar 14, Mar 13, Mar 12).
I want to read data for Previous years also for which I need to click button "Previous Years" on the web page. I would like to get help on how can I click this particular button.
I am not expert in VBA so if someone can throw a piece of code which could click this button then that will be a great help for me.
Thank you!
Below is the code i am using to read web page data:
Function GetMCFinancialData()
Dim XMLReq As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim HTMLTable As MSHTML.IHTMLElement
Dim HTMLTables As MSHTML.IHTMLElementCollection
Dim HTMLRow As MSHTML.IHTMLElement
Dim HTMLRows As MSHTML.IHTMLElementCollection
Dim HTMLCell As MSHTML.IHTMLElement
Dim sText As String
Dim rowNum As Long
Dim colNum As Long
On Error GoTo skipAllFinData
XMLReq.Open "GET", "http://www.moneycontrol.com/financials/aartidrugs/balance-sheetVI/AD#AD", False
XMLReq.send
If XMLReq.Status <> 200 Then
'MsgBox "Error " & XMLReq.Status & ": " & XMLReq.StatusText
Exit Function
End If
HTMLDoc.body.innerHTML = XMLReq.responseText
'Current Data
rowNum = 1
Cells(rowNum, "A").Value = "Various Financical Data"
rowNum = rowNum + 1
On Error GoTo skipFinData
If InStr(1, Qurl, "capital-structure") = 0 Then
' If not capital structure page
Set HTMLRows = HTMLDoc.getElementsByClassName("table4")(2).getElementsByTagName("tr")
Else
Set HTMLRows = HTMLDoc.getElementsByClassName("table4")(1).getElementsByTagName("tr")
End If
For Each HTMLRow In HTMLRows
colNum = 1
For Each HTMLCell In HTMLRow.getElementsByTagName("td")
Cells(rowNum, colNum).Value = HTMLCell.innerText
colNum = colNum + 1
Next HTMLCell
rowNum = rowNum + 1
Next HTMLRow
skipFinData:
skipAllFinData:
Set XMLReq = Nothing
Set HTMLDoc = Nothing
Set HTMLTable = Nothing
Set HTMLTables = Nothing
Set HTMLRow = Nothing
Set HTMLRows = Nothing
Set HTMLCell = Nothing
End Function
I am trying to scrape a web page, I could read most of the data present at current page for all years (Mar 16, Mar 15, Mar 14, Mar 13, Mar 12).
I want to read data for Previous years also for which I need to click button "Previous Years" on the web page. I would like to get help on how can I click this particular button.
I am not expert in VBA so if someone can throw a piece of code which could click this button then that will be a great help for me.
Thank you!
Below is the code i am using to read web page data:
Function GetMCFinancialData()
Dim XMLReq As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim HTMLTable As MSHTML.IHTMLElement
Dim HTMLTables As MSHTML.IHTMLElementCollection
Dim HTMLRow As MSHTML.IHTMLElement
Dim HTMLRows As MSHTML.IHTMLElementCollection
Dim HTMLCell As MSHTML.IHTMLElement
Dim sText As String
Dim rowNum As Long
Dim colNum As Long
On Error GoTo skipAllFinData
XMLReq.Open "GET", "http://www.moneycontrol.com/financials/aartidrugs/balance-sheetVI/AD#AD", False
XMLReq.send
If XMLReq.Status <> 200 Then
'MsgBox "Error " & XMLReq.Status & ": " & XMLReq.StatusText
Exit Function
End If
HTMLDoc.body.innerHTML = XMLReq.responseText
'Current Data
rowNum = 1
Cells(rowNum, "A").Value = "Various Financical Data"
rowNum = rowNum + 1
On Error GoTo skipFinData
If InStr(1, Qurl, "capital-structure") = 0 Then
' If not capital structure page
Set HTMLRows = HTMLDoc.getElementsByClassName("table4")(2).getElementsByTagName("tr")
Else
Set HTMLRows = HTMLDoc.getElementsByClassName("table4")(1).getElementsByTagName("tr")
End If
For Each HTMLRow In HTMLRows
colNum = 1
For Each HTMLCell In HTMLRow.getElementsByTagName("td")
Cells(rowNum, colNum).Value = HTMLCell.innerText
colNum = colNum + 1
Next HTMLCell
rowNum = rowNum + 1
Next HTMLRow
skipFinData:
skipAllFinData:
Set XMLReq = Nothing
Set HTMLDoc = Nothing
Set HTMLTable = Nothing
Set HTMLTables = Nothing
Set HTMLRow = Nothing
Set HTMLRows = Nothing
Set HTMLCell = Nothing
End Function