2017-06-09 37 views
0

我得到当我打电话一个错误:其被定义为Datascrape()获取错误91在运行中的数据刮代码

Sub Datascrape() 
    Dim count, i As Long 
    Dim ie As Object 
    count = Sheets("properties-2017-06-05").Cells(1, 10).Value 
    Sheets("properties-2017-06-05").Range("D7:E" & count).ClearContents 
    For i = 7 To count 
    Set ie = CreateObject("internetexplorer.Application") 
    ie.navigate Sheets("properties-2017-06-05").Cells(i, 3).Value 
    While ie.busy 
     DoEvents 
    Wend 
    'ie.Visible = True 
    Application.Wait (Now + TimeValue("00:00:03")) 
    Sheets("properties-2017-06-05").Cells(i, 4) = 
    'error happens here 
    ie.document.getelementsbyclassname("col-xs-12 viewAllReviews")(0).innertext 
    On Error Resume Next 
    Sheets("properties-2017-06-05").Cells(i, 5) = 
    ie.document.getelementsbyclassname("APGWBigDialChart widget")(0).getElementsByTagName("text")(1).innertext 
    On Error Resume Next 
    ie.Quit 
    Next 
End Sub 

后约3或4次迭代循环的,它抛出一个Error 91和我不懂为什么。

link to picture

+0

对于初学者,您不应该在循环中创建IE *。对于(可能)等待网页加载而言,Application.Wait最多是一种不可靠的方法。那是你想要做什么? –

+0

我需要实际浏览不同的网址。如果我不创建ie对象,那么我得到运行时错误, –

回答

0

运行时,当对象的计算结果为Nothing因为你链接(0)(指数)针对可能不会返回一个有效的对象/集合的表达错误91会发生。

使用这种表达,

ie.document.getelementsbyclassname("col-xs-12 viewAllReviews")(0).innertext 

如果getElementsByClassName方法返回一个空的集合,然后试图索引集合0将提高91错误。

从本质上讲,你的代码写在假定调用此方法将总是结果长度集合> = 1

为什么不这样?

您的等待循环和Application.Wait可能不够。考虑修改等待循环:

While ie.busy and not ie.ReadyState = 4 
    DoEvents 
Wend 

威力足以解决问题。我也建议移动实例化外部的循环,没有理由反复创建循环内的ie对象。

肯定将避免错误,并建议/在调试的:

Sub Datascrape() 
Dim count as Long, i As Long 
Dim ie As Object 
Dim ws as Worksheet 

Set ws = Sheets("properties-2017-06-05") 
Set ie = CreateObject("internetexplorer.Application") 

count = ws.Cells(1, 10).Value 
ws.Range("D7:E" & count).ClearContents 
For i = 7 To count 
    ie.navigate ws.Cells(i, 3).Value 
    While ie.busy and not ie.ReadyState = 4 
     DoEvents 
    Wend 
    Application.Wait (Now + TimeValue("00:00:03")) '## This line probably isn't needed 
    Dim ele 
    Set ele = ie.document.getelementsbyclassname("col-xs-12 viewAllReviews") 
    If ele Is Nothing Then 
     MsgBox "Class name 'col-xs-12 viewAllReviews' isn't found!" 
     ws.Cells(i, 4) = "NOT FOUND" '## Breakpoint here if needed to debug 
    Else: 
     ws.Cells(i, 4) = ele(0).innerText 
    End If 
    On Error Resume Next 
    ws.Cells(i, 5) = _ 
     ie.document.getelementsbyclassname("APGWBigDialChart widget")(0).getElementsByTagName("text")(1).innertext 
    On Error GoTo 0 
Next 
ie.Quit 
End Sub 

注:为了提供更具体的指导,你需要提供例如输入,这将有助于重现错误。

此外,在您的错误处理

记你有On Error Resume Next则表达式,然后On Error Resume Next,你的循环中。这几乎肯定不是你想要的。通常情况下,它是这样的:

On Error Resume Next 
<< expression(s) >> 
On Error GoTo 0 

鉴于你有:

On Error Resume Next 
<< expression(s) >> 
On Error Resume Next 

除非你确实有前,好像你的循环不能可能引发错误除了在第一个迭代。 GoTo 0恢复正常的错误处理,Resume Next仅仅意味着“假装错误没有发生,即使它们是”。它不提供可用于排除故障的诊断信息。作为一个经验法则,应该避免(除少数例外)。你所做的是基本上允许迭代1的错误,但是抑制所有其他错误。

+0

什么行引发错误?并且可以提供示例输入吗? –

+0

运行代码后,我仍然收到错误91。 运行代码后,我仍然收到错误91。 Dim ele ele = ie.document.getelementsbyclassname(“col-xs-12 viewAllReviews”) 如果ele.length = 0那么'错误91发生在这里。如果需要进行调试 –

+0

apartmentratings.com/fl/davie/palm-ranch-apartments_9199332346275159825/ 我需要列出评论和整体评分不同的属性,如网址中提到的属性。 –