Excel VBA:如何通过引用类名来从HTML复制href

<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 

这个代码你应该通过debugging模式来进行…对于教学使用而不是对于生产来说,它会做你想要的。 我使用Google作为testing网站。

 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