获取所有innertext VBA

时间:2017-03-11 22:52:53

标签: excel vba

如何从网页获取所有innerText? - 使用下面的代码只获得第一行,For i = 4 To 8 ThisWorkbook.Worksheets("Sheet1").Cells(3, 20 + i).FormulaR1C1 = "=IF(RC[-11]=0,0,(IF(SUMIF(R3C2:R" & lRow & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")>RC[-11]*1000000, SUMIF(R3C2:R" & lRow1 & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")- RC[-11]*1000000,0)))" Next i “段落”标记停止此读取。我可以添加vCompanyCity等等 - 但无法弄清楚如何在这个方框中获得下一行......

<p>

Here is the website stuff

2 个答案:

答案 0 :(得分:3)

给这样的东西一个机会。您的帖子中缺少一些细节,因此我不得不做出一些假设。

以下有两种方法:

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

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

答案 1 :(得分:0)

这里是我用来查找所有需要抓取,单击并从网页发送信息给自己的标签以及登录并执行其他常规过程(例如输入信息)的方法的示例手动。有了它,您可以使用instr语句来构建更复杂的过程,从而找出您想要搜索,单击等的内容,而无需实际手动进行操作,例如,如果Microsoft或其他公司不断更改Java脚本按钮标记无论按钮的名称/标记是什么,您都可以使用文本字符串和偏移量来单击按钮。

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