innerText返回包含标记

时间:2017-10-19 00:50:23

标签: vba url tags innertext

我对VBA和Webscraping很新。我正在尝试打开XML文档中的URL。

http://research.investors.com/services/AutoSuggest.asmx/GetQuoteResults?q=txn&limit=10

InnerText为我提供了网址,但包括<Url></Url>

<Url>http://research.investors.com/stock-quotes/nasdaq-apple-inc-aapl.htm</Url>

这就是为什么我无法打开URL。 这是我的代码:

    Sub Macro1()

Dim ie As Object


Set Rng = Range("A3:A6")

Set Row = Range(Rng.Offset(1, 0), Rng.Offset(1, 0).End(xlDown))

        Set ie = CreateObject("InternetExplorer.Application")
        With ie
        .Visible = True
        For Each Row In Rng
        .navigate "research.investors.com/services/AutoSuggest.asmx/GetQuoteResults?q=" & Range("A" & Row.Row).Value & "&limit=10"
        Application.Wait (Now + TimeValue("0:00:02"))
        Do
        DoEvents
        Loop Until ie.readyState = READYSTATE_COMPLETE
        Dim doc As HTMLDocument
        Set doc = ie.document
        While ie.readyState <> 4

        Wend


       .navigate doc.getElementsByTagName("Url")(0).innerText




        Next Row
    End With
    ie.Quit
End Sub

我希望有人可以指出我做错了什么。

非常感谢,再见 Nightflight

1 个答案:

答案 0 :(得分:0)

通常,您可以通过3种方式访问​​节点:

  1. 使用getElementsByTagName()方法

  2. 循环遍历(遍历)节点树。

  3. 通过使用节点关系导航节点树。

  4. 我无法实现第一种方法,但下面是后两种方法的4个示例,以及解析字符串并使用Split函数提取所需字符串的第5种方法。

    Option Explicit
    
    Public Sub test()
        Dim http As Object, xml As String            'Dim http As New XMLHTTP60
        Set http = CreateObject("MSXML2.ServerXMLHTTP")
    
        With http
            .Open "GET", "https://research.investors.com/services/AutoSuggest.asmx/GetQuoteResults?q=txn&limit=10", False
            .send
            xml = .responseText
        End With
    
        Dim doc As MSXML2.DOMDocument60
    
        Set doc = New MSXML2.DOMDocument60
    
        If Not doc.LoadXML(xml) Then
            Err.Raise doc.parseError.ErrorCode, , doc.parseError.reason
            Exit Sub
        End If
    
        Debug.Print "Method 1 " & doc.ChildNodes(1).ChildNodes(0).LastChild.Text
        Debug.Print "Method 2 " & doc.ChildNodes(1).ChildNodes(0).ChildNodes(2).Text '<== IXMLDOMElement
    
        Dim node As IXMLDOMElement, node2 As IXMLDOMElement
    
        For Each node In doc.DocumentElement.ChildNodes
            For Each node2 In node.ChildNodes
                If node2.nodeName = "Url" Then
                    Debug.Print "Method 3 " & node2.Text
                    Exit For
                End If
            Next node2
        Next node
    
        Dim m As IXMLDOMNodeList, i As Long
        Set m = doc.DocumentElement.ChildNodes
    
        For i = 0 To doc.DocumentElement.ChildNodes.Length - 1
    
            Dim x As IXMLDOMElement
            Set x = doc.DocumentElement.ChildNodes(i)
            Dim xlen As Long, y As IXMLDOMElement, n As Long
            xlen = x.ChildNodes.Length
            Set y = x.FirstChild
    
            For n = 0 To xlen - 1
                If y.nodeName = "Url" Then
                    Debug.Print "Method 4 " & y.Text
                End If
                Set y = y.NextSibling
            Next n
    
        Next i
    
        Debug.Print "Method 5 " & Split(Split(xml, "<Url>")(1), "</Url")(0)
    
    End Sub