how to get data form html frame but not first div

xyz987

New Member
Joined
Mar 10, 2024
Messages
3
Office Version
  1. 2010
Platform
  1. Windows
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

Snap3.jpg

Snap1.jpg





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
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,223,885
Messages
6,175,179
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top