【发布时间】:2021-08-27 13:45:12
【问题描述】:
我需要遍历特定网站的下拉列表并下载每个条目的最新文件。我设法打开网站并单击“提交”按钮,但我找不到循环遍历所有下拉条目的解决方案。
Dim reportnr As String
Dim internetadress As String
Dim btn As Object
Dim IE As SHDocVw.InternetExplorer
reportnr = 10
internetadress = adress & reportnr
Set IE = CreateObject('InternetExplorer.Application')
IE.Visible = True
IE.navigate internetadress
Do While IE.Busy
Application.Wait DateAdd("s", 2, Now)
Loop
While IE.Busy
DoEvents
Wend
For Each btn In IE.document.getElementsByClassName("btn btn-primary")
If btn.name = "submit" Then
btn.Click
End If
Next btn
网站上关于下面下拉菜单的代码。
我尝试了几种方法,但我会出错或什么也没有发生。我尝试的最后一件事是:
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub SelectQuantity()
Dim ie As New InternetExplorer, numberOfOptions As Long, i As Long
With ie
.Visible = True
.Navigate2 "https://www.amazon.com/belif-True-Cream-Aqua-Korean/dp/B00H4GOAZO/ref=pd_cart_crc_cko_cp_2_2/139-8277217-3794320?_encoding=UTF8&pd_rd_i=B00H4GOAZO&pd_rd_r=e154e278-8a11-4ab0-8173-5d0dbaff1938&pd_rd_w=Hm8FW&pd_rd_wg=Hpv4X&pf_rd_p=eff166ab-25d2-4f2c-a51d-0a0e86061f9d&pf_rd_r=EVT26E6K7CV8T1QMTY7H&psc=1&refRID=EVT26E6K7CV8T1QMTY7H"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
numberOfOptions = .querySelectorAll("#quantity option").Length 'gather option tag element children of parent select element with id quantity
For i = 1 To numberOfOptions
.querySelector("#quantity [value='" & i & "']").Selected = True
ActiveSheet.Cells(i, 1) = i
Next
Stop
End With
End With
End Sub
来源:VBA loop through dropdown elements from web page and download to excel sheet
提前感谢您的帮助。
【问题讨论】:
-
我们看不到 select 元素的相关 html。此外,通过edit 使用 sn-p 工具插入 html 而不是发布图像。此外,当您循环到 .Length 而不是 .Length -1 时,您的代码将失败。尴尬的是,原来的代码是我的错。我已将引用链接的那部分编辑为正确的。道歉。
-
网站是内部的吗?如果没有,您可以提供网站的链接,以便我们进行测试,看看如何提供帮助。如果不能提供链接,请提供下拉列表的详细html代码,而不是图片。
-
好的,对不起,我下次会这样做。提前感谢您的帮助。该网站的链接如下:theice.com/marketdata/reports/10。我们需要每天在下拉菜单中获取文件。
标签: html vba object internet-explorer web-scraping