错误处理程序:未阻止错误

时间:2018-07-13 08:54:47

标签: excel vba excel-vba error-handling

问题是错误处理程序超过了规则,在循环正确运行1次后我仍然收到错误。请帮帮我。

错误发生在行:“ ActiveWorkbook.Sheets(” Final“)。Range(” B2“)。Offset(j,i).Value = objIE.document.getElementsByClassName(HTML_Class)(m).getElementsByTagName(” td“)(i).innerText” 但是,它会在第二次迭代中产生错误,一次正确运行后就会在我需要抓取的网站的第三页上产生错误。

我不明白发生了什么。如果可以的话,请帮帮我。

Sub Web_Scraping()

  ActiveWorkbook.Sheets("Final").Range("A1:Z10000").ClearContents

  Set objIE = CreateObject("InternetExplorer.Application")

  objIE.Top = 0
  objIE.Left = 0
  objIE.Width = 800
  objIE.Height = 600

  objIE.Visible = False

  HTML_Class = ActiveWorkbook.Sheets("Input").Range("C6").Value
  i = 0
  j = 0
  k = -1

Next_Page:
  Err.Clear
  k = k + 1
  Do
    m = 0
    On Error GoTo Error
    'Site masked below for privacy
    Site = "xyz.com"
    objIE.Navigate (Site)

    Do
      DoEvents
    Loop Until objIE.readystate = 4    

    'objIE.document.getElementsByTagName("div")
    Do
      On Error GoTo Next_Page

      For i = 0 To 6
        ActiveWorkbook.Sheets("Final").Range("B2").Offset(j, i).Value = _
          objIE.document.getElementsByClassName(HTML_Class)(m).getElementsByTagName("td")(i).innerText
      Next i
      m = m + 1
      j = j + 1
    Loop Until (Err.Number <> 0)

  Loop Until (Err.Number <> 0)
Error:
  Err.Clear
  MsgBox "Done!"

  objIE.Quit
  Set objIE = Nothing
End Sub

0 个答案:

没有答案