我正在嘗試從網站下方抓取表格。網站左側有一個下拉串列,我想在型別部分選擇并單擊“專業”。下面是我的 VBA 代碼。我沒有網路開發經驗,我所能做的就是找到我想要的表格并選擇顯示 100 個專案

Sub CopyFromHKEXWebsite()
Dim ie As Object, btnmore As Object, tbl As Object
Dim cnt As Integer
Set ie = CreateObject("internetexplorer.application")
With ie
.Visible = True
.navigate "https://www.hkex.com.hk/Market-Data/Securities-Prices/Debt-Securities?sc_lang=en"
Do
DoEvents
Loop While .ReadyState <> 4 Or .Busy
.Document.getelementsbyclassname("select_items")(0).setAttribute "style", "display: block"
Do
DoEvents
Loop While .Busy 'wait for scriptcode to execute
.Document.getelementsbyclassname("select_items")(0).Children(2).Click
Do
DoEvents
Loop While .ReadyState <> 4 Or .Busy
****'professional
.Document.getelementsbyid("select2")(0).selectedindex = 1
Do
DoEvents
Loop While .Busy 'wait for scriptcode to execute****
Do
Set tbl = .Document.getelementsbyclassname("table_debtsecurities")(0)
Set btnmore = .Document.getelementsbyclassname("loadmore")(0)
cnt = cnt 1
Application.Wait Now TimeValue("00:00:01")
Loop While tbl Is Nothing Or btnmore Is Nothing And cnt < 5 'maximum attempts = 4
Do While btnmore.getattribute("style") = ""
btnmore.Click
Do
DoEvents
Loop While .Busy
Loop
End With
End Sub
uj5u.com熱心網友回復:
首先它不是GetElementsById但是GetElementById(單數Element沒有s),因為不能有多個相同的元素ID(不違反 HTML 標準)。雖然 for NameandClassName可以存在多個元素,但它需要是復數Elements和一個s。
其次,您找到組合框,name="select2"因此我們需要使用GetElementsByName
<input type="hidden" name="select2" value="ALL">
Dim TypeCombo As Object
Set TypeCombo = .document.GetElementsByName("select2")(0)
TypeCombo.Value = "Professional"
然后應用按鈕
<div class="etps_apply_btn">APPLY FILTERS</div>
可以通過它找到所以我們使用GetElementsByClassName
Dim ApplyButton As Object
Set ApplyButton = .document.GetElementsByClassName("etps_apply_btn")(0)
ApplyButton.Click
并點擊一下。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/377342.html
上一篇:搜索具有特定文本的所有單元格的范圍并將所有相鄰單元格的值更改為0
下一篇:努力讓VBAShell執行CMD
