Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA:如何通过引用类名从HTML复制href |_Excel_Vba - Fatal编程技术网

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

Excel VBA:如何通过引用类名从HTML复制href |,excel,vba,Excel,Vba,如何通过Excel VBA复制此HTML中的href 这是我的代码,但不起作用 Set Export = ie.Document.all("export excel") URL = Export.href ie.Navigate URL 您应该在调试模式下浏览此代码。。。它更多的是用于教学用途,而不是用于生产,但它可以满足您的需求。我用谷歌作为测试网站 Sub Test() Dim Browser As SHDocVw.InternetExplorer Dim HTMLDoc

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

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

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

您应该在调试模式下浏览此代码。。。它更多的是用于教学用途,而不是用于生产,但它可以满足您的需求。我用谷歌作为测试网站

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
子测试()
将浏览器设置为SHDocVw.InternetExplorer
将HTMLDoc设置为MSHTML.HTMLDocument
Dim链接作为字符串,目标作为对象
链接=”http://www.google.com"
'启动浏览器
设置浏览器=新建SHDocVw.InternetExplorer
Browser.Visible=True
“等等
浏览器。导航链接
“等等
设置HTMLDoc=Browser.Document
“等等
Set Target=GetElementByTagAndClassName(HTMLDoc,“SPAN”,“gbtb2”)
如果不是(目标为零),则
'在此处测试家长是否真的是
Debug.Print Target.parentElement.href
“塔塔阿!!!
如果结束
端接头
'按标记和属性值获取元素
函数GetElementByTagAndClassName(Doc作为MSHTML.HTMLDocument,ByVal标记作为String,ByVal匹配作为String)作为MSHTML.IHTMLElement
Dim ECol作为MSHTML.IHTMLElementCollection
Dim IFld为MSHTML.IHTMLElement
设置GetElementByTagAndClassName=Nothing
Set ECol=Doc.getElementsByTagName(标记)
对于ECol中的每个IFld
'Debug.Print IFld.className
如果IFld.className=匹配,则
设置GetElementByTagAndClassName=IFld
退出功能
如果结束
下一个
端函数

我想您必须迭代
标记,并以某种方式测试href值。您是否可以迭代
标记并使用类名来识别您想要的href,然后可能使用
Mid
Instr
函数从
outerHTML
中提取href?请精确输入问题:您想让
元素的
href
第一次出现在
上吗?!?有可用的测试URL吗?@Jordan再看看。。。a标签没有类。啊,是的,当然!span标记位于
a
标记中,但类名应用于span,而不是
a
。兆字节