Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

excel - Get all innertext VBA

How do I get all of the innerText from a webpage? - Using the code below only gets the first line, the <p> "Paragraph" tag stops this read. I'd be okay adding vCompanyCity, etc - but can't figure out how to get the next line in this box...

Set ie = CreateObject("InternetExplorer.application")
ie.navigate ("https://www.xxxx.com/")
While ie.Busy Or ie.ReadyState <> 4: DoEvents: Wend
vCompanyAddress(i - 1) = ie.document.all("header-left-box").innerText
more code....
End Sub

Here is the website stuff

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...