名称为

时间:2019-12-17 17:40:06

标签: excel vba web-scraping foreach

我让下面的代码正常工作,但是如您所见,它花费了很多时间。因为我需要确保所有结果都已加载。我想我正在寻找循环?

Sub LaadAllePaginaenScrape()
    Dim i As Long
    Dim URL As String
    Dim ie As Object
    Dim objElement As Object
    Dim objCollection As Object
    'Dim ieObj As InternetExplorer
    Dim htmlEle As IHTMLElement
    Dim e As Integer

e = 1

Application.ScreenUpdating = False

Set ie = CreateObject("InternetExplorer.Application")

ie.Visible = False

URL = "Somepage"

ie.navigate URL

Application.StatusBar = URL & " is loading. Please wait..."

Do While ie.readyState = 4: DoEvents: Loop
Do Until ie.readyState = 4: DoEvents: Loop

Application.StatusBar = URL & " Loaded"

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)


Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)


Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)


Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)


Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")
botao.Click (1)

Application.Wait (Now + TimeValue("0:00:03"))

For Each htmlEle In ie.document.getElementsByClassName("table")(0).getElementsByTagName("tr")
    With ActiveSheet
        .Range("A" & e).Value = htmlEle.Children(0).textContent
        .Range("B" & e).Value = htmlEle.Children(3).textContent
        .Range("C" & e).Value = htmlEle.Children(4).textContent
    End With

    e = e + 1
Next htmlEle

Application.ScreenUpdating = True

End Sub

VBA代码中使用以下代码来单击特定按钮:

<button class="btn btn-primary center-block ng-binding" 
     ng-disabled="!lastCallHadData" ng-click="addPage()" 
     data-ajaxform-clicked="">
    <span ng-show="loading" 
        class="glyphicon glyphicon-refresh spinning ng-hide"></span> 
       Laad meer resultaten
</button>

但是我想循环直到代码中的标记中禁用了属性(?)。

<button class="btn btn-primary center-block ng-binding" 
     ng-disabled="!lastCallHadData" ng-click="addPage()" 
     data-ajaxform-clicked="" disabled="disabled"> << Here you have the attribute disabled
      <span ng-show="loading" 
        class="glyphicon glyphicon-refresh spinning ng-hide"></span> 
           Meer resultaten niet gevonden
 </button>

1 个答案:

答案 0 :(得分:0)

您的代码中有很多小错误,没有html代码,我无法说更多。如果还有其他问题,则最好发布该URL。您可以尝试以下方法:

编辑:重新排序宏代码[循环边界,获取数据,单击按钮和IE循环外]。抱歉,造成混乱。

Option Explicit

Sub LaadAllePaginaenScrape()

  Dim URL As String
  Dim ie As Object
  Dim botao As Object
  Dim htmlEle As Object
  Dim e As Long

  e = 1
  URL = "Somepage"

  Set ie = CreateObject("InternetExplorer.Application")
  ie.Visible = True
  ie.navigate URL
  Application.StatusBar = URL & " is loading. Please wait..."
  Do Until ie.readyState = 4: DoEvents: Loop
  Application.StatusBar = URL & " Loaded"
  Application.Wait (Now + TimeValue("0:00:03"))

  Do
    For Each htmlEle In ie.document.getElementsByClassName("table")(0).getElementsByTagName("tr")
      With ActiveSheet
        .Range("A" & e).Value = htmlEle.Children(0).textContent
        .Range("B" & e).Value = htmlEle.Children(3).textContent
        .Range("C" & e).Value = htmlEle.Children(4).textContent
      End With
      e = e + 1
    Next htmlEle

    Set botao = ie.document.getElementsByClassName("btn btn-primary center-block ng-binding")(0)
    botao.Click (1)
    Application.Wait (Now + TimeValue("0:00:03"))
  Loop Until botao.hasAttribute("disabled")

  ie.Quit
  Set ie = Nothing
  Set botao = Nothing
  Set htmlEle = Nothing
End Sub