gpt4 book ai didi

Excel VBA : How to copy href from HTML by referencing the class name

转载 作者:行者123 更新时间:2023-12-03 03:50:20 25 4
gpt4 key购买 nike

<a href="?contractDate=&amp;6578706f7274=1&amp;currency=MYR&amp;currPage=1&amp;method.searchM2MTotal=Retrieve&amp;d-448075-e=2&amp;netType=M&amp;contractFromDate=13%2F05%2F2016&amp;contractToDate=13%2F05%2F2016"><span class="export excel">Excel </span></a>| 

如何通过 Excel VBA 复制此 HTML 中的 href?

这是我的代码,但它不起作用。

Set Export = ie.Document.all("export excel")
URL = Export.href
ie.Navigate URL

最佳答案

您应该在 Debug模式下浏览此代码...它更多的是用于指导用途而不是用于生产,但它会执行您想要的操作。我使用 Google 作为测试网站。

Sub Test()
Dim Browser As SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim Link As String, Target As Object

Link = "http://www.google.com"

' start browser
Set Browser = New SHDocVw.InternetExplorer
Browser.Visible = True
' wait a bit

Browser.Navigate Link
' wait a bit

Set HTMLDoc = Browser.Document
' wait a bit

Set Target = GetElementByTagAndClassName(HTMLDoc, "SPAN", "gbtb2")

If Not (Target Is Nothing) Then
' test here if parent really is a <a>
Debug.Print Target.parentElement.href
' ta-taaaa!!!
End If

End Sub

' get element by tag and attribute value
Function GetElementByTagAndClassName(Doc As MSHTML.HTMLDocument, ByVal Tag As String, ByVal Match As String) As MSHTML.IHTMLElement
Dim ECol As MSHTML.IHTMLElementCollection
Dim IFld As MSHTML.IHTMLElement


Set GetElementByTagAndClassName = Nothing

Set ECol = Doc.getElementsByTagName(Tag)
For Each IFld In ECol
' Debug.Print IFld.className
If IFld.className = Match Then
Set GetElementByTagAndClassName = IFld
Exit Function
End If
Next
End Function

关于Excel VBA : How to copy href from HTML by referencing the class name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37206910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com