2017-01-05 110 views
-3

我的意圖是抓取該頁面中的所有應用程序名稱以及導致下一頁的應用程序鏈接。但是,當我運行它時,我看到循環後,它會產生以下錯誤「運行時錯誤91,對象變量或塊變量未設置」。這裏是完整的代碼。任何幫助真的會被讚賞。提前致謝。運行時錯誤91,對象變量或未設置塊變量

Sub app_crawler() 
    Dim xmlpage As New XMLHTTP60, htmldoc As New HTMLDocument 
    Dim htmlas As Object, htmla As Object, sstr As String 

    xmlpage.Open "GET", "https://itunes.apple.com/us/app/candy-crush-saga/id553834731?mt=8", False 
    xmlpage.send 
    htmldoc.body.innerHTML = xmlpage.responseText 

    For Each htmlas In htmldoc.getElementsByClassName("lockup-info")(0).getElementsByTagName("a") 
     sstr = htmlas.href 

     xmlpage.Open "GET", sstr, False 
     xmlpage.send 
     htmldoc.body.innerHTML = xmlpage.responseText 

     For Each htmla In htmldoc.getElementsByClassName("intro")(1).getElementsByTagName("h1") 
      x = x + 1: Cells(x, 1) = htmla.innerText 
     Next htmla 
    Next htmlas 
End Sub 
+0

您會在哪一行發生錯誤? – Vityata

+1

*我只是複製完整的代碼,並粘貼在這裏,以避免混淆我所問* - 不幸的是,你基本上給了我們一個程序,並說「它不工作」,沒有給我們任何指示,確切地說問題可能出在哪裏,或者表現出任何形式的努力來嘗試將問題隔離到單個特定指令。你可以嘗試這樣做,並且[編輯]你的問題? –

+1

你需要** [編輯] **信息*到你的問題*。 –

回答

0

這是解決所有的答案我遇到的問題:

Sub app_crawler() 
    Dim http As New XMLHTTP60, hdoc As New HTMLDocument, hdoc_one As New HTMLDocument 
    Dim elem As Object, post As Object, sstr As String 

    With http 
     .Open "GET", "https://itunes.apple.com/us/app/candy-crush-saga/id553834731?mt=8", False 
     .send 
     hdoc.body.innerHTML = .responseText 
    End With 

    For Each elem In hdoc.getElementsByClassName("lockup-info") 
     With elem.getElementsByTagName("li")(0).getElementsByTagName("a") 
      If .Length Then sstr = .Item(0).href 
     End With 
     With http 
      .Open "GET", sstr, False 
      .send 
      hdoc_one.body.innerHTML = .responseText 
     End With 

     For Each post In hdoc_one.getElementsByClassName("intro") 
      With post.getElementsByTagName("h1") 
       If .Length Then i = i + 1: Cells(i, 1) = .Item(0).innerText 
      End With 
     Next post 
    Next elem 
End Sub 
1

由於Mat's Mugcommented,你必須測試,如果htmlas(x)回報Nothing從中獲取元素之前,同樣適用於getElementByTagName及其他:

Sub TestSth() 
    Dim xmlpage As New MSXML2.XMLHTTP60 
    Dim htmldoc As New MSHTML.HTMLDocument 
    Dim htmlas As Object, gist As Object 
    Dim htmla As Object, main As Object, lux As String 
    Dim x As Long, link As Object, thank As Object 
    Range("A1").Select 
    xmlpage.Open "GET", "https://itunes.apple.com/us/app/candy-crush-saga/id553834731?mt=8", False 
    xmlpage.send 
    htmldoc.body.innerHTML = xmlpage.responseText 
    Set xmlpage = Nothing 

    Set htmlas = htmldoc.getElementsByClassName("lockup-info") 
    For x = 0 To htmlas.Length - IIf(htmlas.Length > 0, 1, 0) 
    If Not htmlas(x) Is Nothing Then 
     If Not htmlas(x).getElementsByTagName("a") Is Nothing Then 
     If Not htmlas(x).getElementsByTagName("a")(0) Is Nothing Then 
      lux = htmlas(x).getElementsByTagName("a")(0).getAttribute("href") 
      If lux <> "" Then 
      xmlpage.Open "GET", lux, False 
      xmlpage.send 
      htmldoc.body.innerHTML = xmlpage.responseText 

      Set main = htmldoc.getElementsByClassName("intro")(1) 
      Set thank = main.getElementsByTagName("div") 
      For Each link In thank 
       ActiveCell.Value = link.innertext 
       ActiveCell.Offset(1, 0).Select 
      Next link 
      End If 
     End If 
     End If 
    End If 
    Next x 
End Sub 
+0

感謝您的努力,但它並沒有拉動它應該的整個數據。我昨晚已經解決了這個問題。這是我期待的: – SIM

相關問題