【发布时间】:2022-01-13 03:49:49
【问题描述】:
我正在尝试从以下网站抓取表格。网站左侧有一个下拉列表,我想选择并单击类型部分中的“专业”。下面是我的 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
【问题讨论】:
-
这个问题有一个捷径,只要把网站改成
https://www.hkex.com.hk/Market-Data/Securities-Prices/Debt-Securities?sc_lang=en&type=0(在“sc_lang=en”后面加“&type=0”)然后网站会自动转换成“专业”表,但我想了解更多使用 VBA 的工作原理.click函数
标签: html excel vba web-scraping