2017-05-29 130 views
2

我想要聲明一個節點數組(這不是問題),然後在數組的每個元素內部刮兩個子節點的innerHTML - 以SE爲例使用對象方法IE),假設我試圖在主頁上提取標題和問題摘要,則有一個節點數組(類名稱:「問題摘要」)。從使用VBA的站點刮掉innerHTML

有那麼兩個孩子節點(瓦 - 類名稱:「問題 - 超鏈接」和提取物 - 類名稱:「摘錄」)我正在使用的代碼是如下:

Sub Scraper() 
Dim ie As Object 
Dim doc As Object, oQuestionShells As Object, oQuestionTitle As Object, oQuestion As Object, oElement As Object 
Dim QuestionShell As String, QuestionTitle As String, Question As String, sURL As String 

Set ie = CreateObject("internetexplorer.application") 
sURL = "https://stackoverflow.com/questions/tagged/excel-formula" 

QuestionShell = "question-summary" 
QuestionTitle = "question-hyperlink" 
Question = "excerpt" 

With ie 
    .Visible = False 
    .Navigate sURL 
End With 

Set doc = ie.Document 'Stepping through so doc is getting assigned (READY_STATE = 4) 

Set oQuestionShells = doc.getElementsByClassName(QuestionShell) 

For Each oElement In oQuestionShells 
    Set oQuestionTitle = oElement.getElementByClassName(QuestionTitle) 'Assigning this object causes an "Object doesn't support this property or method" 
    Set oQuestion = oElement.getElementByClassName(Question) 'Assigning this object causes an "Object doesn't support this property or method" 
    Debug.Print oQuestionTitle.innerHTML 
    Debug.Print oQuestion.innerHTML 
Next 

End Sub 

回答

2

getElementByClassName不是一種方法。

您只能使用getElementsByClassName(注意方法名稱中的複數形式),它返回IHTMLElementCollection

使用Object代替IHTMLElementCollection是好的 - 但您仍然需要通過提供索引來訪問集合中的特定元素。

我們假設每個oElement只有一個question-summary類的實例和question-hyperlink類的一個實例。然後,您可以僅使用getElementsByClassName並在末尾使用(0)來提取返回的數組的第一個元素。

所以,你的代碼修正:

Set oQuestionTitle = oElement.getElementsByClassName(QuestionTitle)(0) 
Set oQuestion = oElement.getElementsByClassName(Question)(0) 

全部工作的代碼(有一些更新,即使用Option Explicit,等待頁面加載):

Option Explicit 

Sub Scraper() 

    Dim ie As Object 
    Dim doc As Object, oQuestionShells As Object, oQuestionTitle As Object, oQuestion As Object, oElement As Object 
    Dim QuestionShell As String, QuestionTitle As String, Question As String, sURL As String 

    Set ie = CreateObject("internetexplorer.application") 
    sURL = "https://stackoverflow.com/questions/tagged/excel-formula" 

    QuestionShell = "question-summary" 
    QuestionTitle = "question-hyperlink" 
    Question = "excerpt" 

    With ie 
     .Visible = True 
     .Navigate sURL 
     Do 
      DoEvents 
     Loop While .ReadyState < 4 Or .Busy 
    End With 

    Set doc = ie.Document 

    Set oQuestionShells = doc.getElementsByClassName(QuestionShell) 

    For Each oElement In oQuestionShells 
     'Debug.Print TypeName(oElement) 

     Set oQuestionTitle = oElement.getElementsByClassName(QuestionTitle)(0) 
     Set oQuestion = oElement.getElementsByClassName(Question)(0) 

     Debug.Print oQuestionTitle.innerHTML 
     Debug.Print oQuestion.innerHTML 
    Next 

    ie.Quit 

End Sub 
+0

我是個白癡!謝謝 :) – Jeremy