Dear sirs
i want to get stock dividend from this website 台泥(1101.TW) 股利政策 - Yahoo奇摩股市, there is the same body name div class="table-body-wrapper" , but i only want to get number 4. i try use code like list, but it can't only catch no.4 body. can give me some suggest
Sub Hi_stock()
Workbooks("test1.xlsm").Activate
Sheets.Add(After:=ActiveSheet).Name = "Dividend"
'Dim HTTPRequest As MSXML2.XMLHTTP
Dim httpRequest As Object
Dim htmlDoc As Object
Dim tables As Variant
Dim name_get As Variant
Dim dividend As Variant
Dim colNum As Integer
Dim rowNum As Integer
Dim data_1 As MSHTML.HTMLTable
Dim data_2 As MSHTML.HTMLTableRow
Set httpRequest = CreateObject("MSXML2.XMLHTTP")
Set htmlDoc = CreateObject("htmlfile")
url_1 = "台泥(1101.TW) 股利政策 - Yahoo奇摩股市"
httpRequest.Open "GET", url_1, False
httpRequest.send
'Check if the request was successful (status code 200)
If httpRequest.Status = 200 Then
'Create a new HTML document object
htmlDoc.body.innerHTML = httpRequest.responseText
'get stock name
Set name_get = htmlDoc.getElementsByTagName("div")
For Each tbl In name_get
If tbl.className = "D(f) Ai(c) Mb(6px)" Then
For Each tblCol In tbl.getElementsByTagName("h1")
Cells(1, 1).Value = tblCol.innerText
Debug.Print (tblCol.innerText)
Next tblCol
End If
Next tbl
'get dividend title
Set dividend = htmlDoc.getElementsByTagName("div")
For Each tb_1 In dividend
If tb_1.className = "table-header Ovx(s) Ovy(h) W(100%)" Then
rowNum = 2
For Each tb_1_1 In tb_1.getElementsByTagName("div")
colNum = 1
For Each tb_1_2 In tb_1_1.getElementsByTagName("div")
Cells(rowNum, colNum).Value = tb_1_2.innerText
colNum = colNum + 1
Next tb_1_2
Next tb_1_1
Exit For
End If
Next tb_1
'get dividend data
'With htmlDoc.getElementsByTagName("table-body-wrapper")(10)
Set data_1 = dividend.getElementsByTagName("table-body-wrapper")(4)
Set data_1 = dividend(4).getElementsByTagName("table-body-wrapper")
x = 1
For Each data_2 In data_1.getElementsByTagName("ul")
For Each data_3 In data_2.getElementsByTagName("li")
Cells(x, y).Value = data_3.innerText
y = y + 1
Next data_3
x = x + 1
Next data_2
'End With
End If
End Sub
i want to get stock dividend from this website 台泥(1101.TW) 股利政策 - Yahoo奇摩股市, there is the same body name div class="table-body-wrapper" , but i only want to get number 4. i try use code like list, but it can't only catch no.4 body. can give me some suggest
Sub Hi_stock()
Workbooks("test1.xlsm").Activate
Sheets.Add(After:=ActiveSheet).Name = "Dividend"
'Dim HTTPRequest As MSXML2.XMLHTTP
Dim httpRequest As Object
Dim htmlDoc As Object
Dim tables As Variant
Dim name_get As Variant
Dim dividend As Variant
Dim colNum As Integer
Dim rowNum As Integer
Dim data_1 As MSHTML.HTMLTable
Dim data_2 As MSHTML.HTMLTableRow
Set httpRequest = CreateObject("MSXML2.XMLHTTP")
Set htmlDoc = CreateObject("htmlfile")
url_1 = "台泥(1101.TW) 股利政策 - Yahoo奇摩股市"
httpRequest.Open "GET", url_1, False
httpRequest.send
'Check if the request was successful (status code 200)
If httpRequest.Status = 200 Then
'Create a new HTML document object
htmlDoc.body.innerHTML = httpRequest.responseText
'get stock name
Set name_get = htmlDoc.getElementsByTagName("div")
For Each tbl In name_get
If tbl.className = "D(f) Ai(c) Mb(6px)" Then
For Each tblCol In tbl.getElementsByTagName("h1")
Cells(1, 1).Value = tblCol.innerText
Debug.Print (tblCol.innerText)
Next tblCol
End If
Next tbl
'get dividend title
Set dividend = htmlDoc.getElementsByTagName("div")
For Each tb_1 In dividend
If tb_1.className = "table-header Ovx(s) Ovy(h) W(100%)" Then
rowNum = 2
For Each tb_1_1 In tb_1.getElementsByTagName("div")
colNum = 1
For Each tb_1_2 In tb_1_1.getElementsByTagName("div")
Cells(rowNum, colNum).Value = tb_1_2.innerText
colNum = colNum + 1
Next tb_1_2
Next tb_1_1
Exit For
End If
Next tb_1
'get dividend data
'With htmlDoc.getElementsByTagName("table-body-wrapper")(10)
Set data_1 = dividend.getElementsByTagName("table-body-wrapper")(4)
Set data_1 = dividend(4).getElementsByTagName("table-body-wrapper")
x = 1
For Each data_2 In data_1.getElementsByTagName("ul")
For Each data_3 In data_2.getElementsByTagName("li")
Cells(x, y).Value = data_3.innerText
y = y + 1
Next data_3
x = x + 1
Next data_2
'End With
End If
End Sub