2017-06-14 46 views
0

我想要一個webquery工作,但它似乎只是不會。 我的目標是抓取網頁的源代碼。不幸的是,該網站似乎有不同的幀,這就是爲什麼我的代碼無法正常工作。因此,我試圖修改一個我在網上找到的應該解決Frame問題的代碼。問題是,代碼不起作用,也許是因爲它看起來有點老。誰能幫我?VBA - 網絡查詢 - 多個幀

下面的代碼創建錯誤(需要objecet)爲: 「設置profileFrame .document.getElementById(」 profileFrame 「)」

公用Sub IE_Automation()

'Needs references to Microsoft Internet Controls and Microsoft HTML Object Library 

Dim baseURL As String 
Dim IE As InternetExplorer 
Dim HTMLdoc As HTMLDocument 
Dim profileFrame As HTMLIFrame 
Dim slotsDiv As HTMLDivElement 

'example URL with multiple frames 
baseURL = "https://www.xing.com/search/members?section=members&keywords=IT&filters%5Bcontact_level%5D=non_contact" 

Set IE = New InternetExplorer 
With IE 
    .Visible = True 

    'Navigate to the main page 

    .navigate baseURL & "/publictrophy/index.htm?onlinename=ace_anubis" 
    While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend 

     'Get the profileFrame iframe and navigate to it 

     Set profileFrame = .document.getElementById("profileFrame") 

     .navigate baseURL & profileFrame.src 
     While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend 

      Set HTMLdoc = .document 
     End With 

     'Display all the text in the profileFrame iframe 

     MsgBox HTMLdoc.body.innerText 

     'Display just the text in the slots_container div 

     Set slotsDiv = HTMLdoc.getElementById("slots_container") 
     MsgBox slotsDiv.innerText 

    End Sub 

最良好的祝願, 安德烈亞斯

回答

0

Hummmm,我不完全確定你在這裏做什麼,但你可以嘗試下面的代碼嗎?

Option Explicit 

Sub Sample() 
    Dim ie As Object 
    Dim links As Variant, lnk As Variant 
    Dim rowcount As Long 

    Set ie = CreateObject("InternetExplorer.Application") 
    ie.Visible = True 
    ie.navigate "https://www.xing.com/search/members?section=members&keywords=IT&filters%5Bcontact_level%5D=non_contact" 

    'Wait for site to fully load 
    'ie.Navigate2 URL 
    Do While ie.Busy = True 
     DoEvents 
    Loop 

    Set links = ie.document.getElementsByTagName("a") 

    rowcount = 1 

    With Sheets("Sheet1") 
     For Each lnk In links 
     'Debug.Print lnk.innerText 
      'If lnk.classname Like "*Real Statistics Examples Part 1*" Then 
       .Range("A" & rowcount) = lnk.innerText 
       rowcount = rowcount + 1 
       'Exit For 
      'End If 
     Next 
    End With 
End Sub 
+0

嗨ryguy72,非常感謝!不幸的是我收到一個錯誤「我們的範圍」。我爲這個問題做了一個工作,現在我使用imacros chrome插件來取消內容,然後通過VBA將其導入到excel中。但是,當然,直接的方式可能會更方便一些。祝好運安德烈亞斯 – Andreas

+0

當您逐步瀏覽代碼時,通過反覆敲擊F8,錯誤「我們的範圍」發生在哪裏?哪一行會引發錯誤?這應該是一個線索。雖然,我不能猜測錯誤會在哪裏?我沒有看到任何可疑的信息,一切正常。 – ryguy72