2014-02-25 21 views
2

想從this page如何使用VBA獲取來自IMG的ALT值

獲取價格表爲此,我有以下代碼:

一切工作正常只的的IMG ALT標籤最後一列沒有顯示在。這段代碼非常好,只有最後一列的類沒有被抓取。

Sub TableExample() 

Dim IE As Object 
Dim doc As Object 
Dim strURL As String 

    If Range("B2").Value <> "NA" Then 
     strURL = "http://www.idealo.co.uk/compare/351072/canon-500d-77mm-close-up-lens.html" 
     Set IE = CreateObject("InternetExplorer.Application") 
     With IE 
      '.Visible = True 
      .navigate strURL 
      Do Until .readyState = 4: DoEvents: Loop 
      Do While .Busy: DoEvents: Loop 
      Set doc = IE.document 
      GetAllTables doc 
      .Quit 
     End With 
    End If 

End Sub 

Sub GetAllTables(doc As Object) 

    Dim ws As Worksheet 
    Dim rng As Range 
    Dim tbl As Object 
    Dim rw As Object 
    Dim cl As Object 
    Dim tabno As Long 
    Dim nextrow As Long 
    Dim i As Long 

    Set ws = Sheets("Sheet1") 

    For Each tbl In doc.getElementsByTagName("TABLE") 
     tabno = tabno + 1 
     nextrow = nextrow + 1 
     Set rng = ws.Range("B" & nextrow) 
     rng.Offset(, -1) = "Table " & tabno 
     On Error GoTo Err1: 
     If tabno = 10 Then 
      For Each rw In tbl.Rows 
       colno = 6 
       For Each cl In rw.Cells 
        If colno = 6 And nextrow > 10 Then 
         Set classColl = doc.getElementsByClassName("cellborder") 
         Set imgTgt = classColl(nextrow - 11).getElementsByTagName("img") 
         rng.Value = imgTgt(0).getAttribute("alt") 
        Else 
         rng.Value = cl.innerText 
        End If 
        Set rng = rng.Offset(, 1) 
        i = i + 1 
        colno = colno + 1 
       Next cl 
       nextrow = nextrow + 1 
       Set rng = rng.Offset(1, -i) 
       '  Call trim1 
       i = 0 
      Next rw 
      Exit Sub 
     End If 
    Next tbl 

Err1: 
'Call comp 
' ws.Cells.ClearFormats 
End Sub 
+1

它是'getElement ** s ByClassName',所以你的'classColl'應該是一個集合或類似的東西,所以你可能想嘗試'classColl(0).getElementsByTagName(「img」)'。 – Passerby

+0

我做到了,但結果是一樣的...沒有變化 – user3305327

+0

當你在這裏發佈它時,請讓你的代碼易讀。如果你沒有正確縮進和寫入,很難弄清楚你的代碼是怎麼回事。 :)我這次爲你編輯它。 :) – Manhattan

回答

0

嘗試爲您GetAllTables這個子程序(很髒)變化:

Sub GetAllTables(doc As Object) 

    Dim ws As Worksheet 
    Dim rng As Range 
    Dim tbl As Object 
    Dim rw As Object 
    Dim cl As Object 
    Dim tabno As Long 
    Dim nextrow As Long 
    Dim i As Long 

    Set ws = Sheets("Sheet1") 

    'Improvised way of getting images. 
    Dim imagesColl As New Collection 
    Set imgColl = doc.getElementsByClassName("noborder") 
    For Each imgElem In imgColl 
     If imgElem.getAttribute("height") = 30 And imgElem.getAttribute("width") = 80 Then 
      imagesColl.Add imgElem.getAttribute("alt") 
     End If 
    Next imgElem 

    For Each tbl In doc.getElementsByTagName("table") 
     tabno = tabno + 1 
     If tabno = 10 Then 
      nextrow = 1 
      imgIter = 1 
      For Each rw In tbl.Rows 
       colno = 1 
       For Each cl In rw.Cells 
        Set rng = ws.Cells(nextrow, colno) 
        If colno = 5 Then 
         rng.Value = imagesColl.Item(imgIter) 
         imgIter = imgIter + 1 
        Else 
         rng.Value = cl.innerText 
        End If 
        colno = colno + 1 
       Next cl 
       nextrow = nextrow + 1 
      Next rw 
      Exit Sub 
     End If 
    Next tbl 

End Sub 

事情是,你真的沒有做表格樣式。如果您知道要定位哪些元素,則爲DOM之外的數據創建集合(即,使用正常的 VBA集合)要好得多。

無論如何,以上是嘗試和測試。讓我們知道這是否有幫助。

+0

你真的是一個天才夥計...感謝您的啓發我! – user3305327

0

所有您需要做的是指定要查找哪個ClassColl的圖像。

試試這個:

Set classColl = doc.getElementsByClassName("cellborder") 
Set imgTgt = classColl(0).getElementsByTagName("img") 
Rng.Value = imgTgt(0).getAttribute("alt") 
+0

嘗試過但沒有區別...我想我應該與你分享整個代碼...編輯問題,請檢查一次 – user3305327