Get all innertext VBA

前端 未结 2 1570
我在风中等你
我在风中等你 2020-12-21 07:15

How do I get all of the innerText from a webpage? - Using the code below only gets the first line, the

\"Paragraph\" tag stops this read. I\'d be oka

相关标签:
2条回答
  • 2020-12-21 07:26

    Here is a sample of the method I used to find all the tags I needed to scrape, click and send information from a webpage to myself as well as logging in and doing other routine processes like entering information instead of having to do it manually. With this you can build more complex proceedures using instr statements to figure out what you want to search for, click, etc without having to actually do anything manually so that, for example, if microsoft or some other company keeps changing the java script button tag you can use text strings and offsets to click the button no matter what its name/tag is.

    Sub BIG_EskimoRoll()
    
    On Error Resume Next
        Dim ExpR As Object
        Set ExpR = ie.Document.getElementsByTagName("p")
    
        i = 0
        While i < ExpR.Length
            If ExpR(i).Name <> "" Then
                If ExpR(i).className = "expireFeature" Then msg = ExpR(i).innerText
                
                ''''this is good code to keep around'''''''''''
                'If ExpR(i).className = "expireFeature" Then Debug.Print ExpR(i).className
                'If ExpR(i).className = "expireFeature" Then Debug.Print ExpR(i).innerText
                ' Set text for search
                Debug.Print "Ptsn: " & i
                Debug.Print "Nm: " & ExpR(i).Name
                Debug.Print "Type: " & ExpR(i).Type
                Debug.Print "Vl: " & ExpR(i).Value
                Debug.Print "ID: " & ExpR(i).ID
                Debug.Print "inTxt: " & ExpR(i).innerText
                Debug.Print "inHTML: " & ExpR(i).innerHTML
                Debug.Print "outHTML: " & ExpR(i).outerHTML
                Debug.Print "cNm: " & ExpR(i).className
                Debug.Print "tNm: " & ExpR(i).tagName
                Debug.Print "href: " & ExpR(i).href
                
            End If
            i = i + 1
        Wend
    
    End Sub
    
    0 讨论(0)
  • 2020-12-21 07:32

    Give something like this a shot. There are some details missing in your post, so I had to make some assumptions.

    There are two approaches below:

    1) Use getElementByID and see if the InnerText returns

    2) Use getElementByID and then iterate the paragraph tags.

    Public Sub test()
        Dim ie              As Object
        Dim vCompanyAddress As Variant
        Dim i               As Long: i = 0
        Dim Elements        As Object
        Dim Element         As Object
    
        ReDim vCompanyAddress(1000) ' Not sure how big this array should be
        Set ie = CreateObject("InternetExplorer.Application")
    
        With ie
            .navigate ("https://www.xxxx.com/")
            While .Busy Or .ReadyState <> 4: DoEvents: Wend
            'You can try two things, this:
            vCompanyAddress(i) = .document.getElementById("header-left-box").innerText
    
            'Or you can try this, get the element then create an element
            'collection with all paragraphs tags
            Set Elements = .document.getElementById("header-left-box").getElementsByTagName("p")
    
            For Each Element In Elements
                vCompanyAddress(i) = Element.innerText
                i = i + 1
            Next
        End With
    End Sub
    
    0 讨论(0)
提交回复
热议问题