获取所有内文 VBA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42741997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:16:37  来源:igfitidea点击:

Get all innertext VBA

excelvba

提问by Steve Britton

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...

如何从网页中获取所有的innerText?- 使用下面的代码只获取第一行,<p>“段落”标签停止读取。我可以添加 vCompanyCity 等 - 但无法弄清楚如何在此框中获取下一行...

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

这是网站的东西

采纳答案by Ryan Wildry

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

1) 使用 getElementByID 并查看 InnerText 是否返回

2) Use getElementByID and then iterate the paragraph tags.

2) 使用 getElementByID 然后迭代段落标签。

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