【发布时间】:2017-09-01 18:05:56
【问题描述】:
我正在使用 VBA 导航到 IE 中的网页。网页的元素会在几秒钟后动态加载。 我正在尝试使用带有该网页中链接的树结构。所以我尝试使用以下代码查看内部 HTML,
Sub treemenutest()
Dim ie As SHDocVw.InternetExplorerMedium
Dim doc As MSHTML.HTMLDocument
Dim elements3 As MSHTML.IHTMLElementCollection
Dim nameValueInput3 As MSHTML.HTMLInputElement
Dim LIS As IHTMLElementCollection
Dim LI As IHTMLElement
Set ie = New SHDocVw.InternetExplorerMedium
ie.Visible = True
ie.navigate Sheet1.TextBox1.Value 'web page URL
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
Set doc = ie.document
Set elements3 = doc.getElementsByName("OK")
If Not elements3 Is Nothing Then
Set nameValueInput3 = elements3(1)
If Not nameValueInput3 Is Nothing And nameValueInput3.Type = "submit" Then _
nameValueInput3.Click
End If
Set LIS = ie.document.getElementsByTagName("a")
For i = 0 To LIS.Length - 1
Debug.Print LI.innerHTML
Next
End Sub
Debug.Print 不在即时窗口中显示任何信息。是编码有问题还是因为网页的某些内容是动态的?
任何信息都会有所帮助。
【问题讨论】:
-
您是否可以透露该网页的链接以获得更快的解决方案?
-
这是一个官方网站,所以我不能透露。
-
您会发现使用浏览器的开发工具(按 F12 打开)更容易——只需单击感兴趣的元素即可显示当前的 HTML。
-
@Tim Williams 实际上当我在一个元素上按 F12 时,我可以看到带有标签“a”的元素。但我无法使用我的 VBA 访问那些。所以我想弄清楚这些元素的索引。
标签: html vba internet-explorer web-scraping