2016-12-28 32 views
0

我想網頁鏈接和嵌套標籤的元素:getElement通過嵌套的標籤名稱在VBA

<a href='/cacti/graph.php?action=view&amp;local_graph_id=279&amp;rra_id=all'><img class='graphimage' id='graph_279' src='/cacti/graph_image.php?local_graph_id=279&amp;rra_id=0&amp;view_type=tree&amp;graph_start=1482822537&amp;graph_end=1482908937' border='0' alt='ARD-Besat-MKT450G - Bandwidth 16 Mb- ether3-Link to PTMP'></a> 

這是我的源代碼的一部分。

最後,我想:

cells(1,1).value="link address : /cacti/graph.php?action=view&amp;local_graph_id=279&amp;rra_id=all" 

cells(1,2).value="image address: /cacti/graph_image.php?local_graph_id=279&amp;rra_id=0&amp;view_type=tree&amp;graph_start=1482822537&amp;graph_end=1482908937" 

cells(1,3).value="image alt: ARD-Besat-MKT450G - Bandwidth 16 Mb- ether3-Link to PTMP' 

我在VBA函數爲:

Function WebPageLinks(internet, tagname As String) 
Dim internetdata As HTMLDocument 
Dim internetlink As Object 
Dim internetinnerlink As Object 
Set internetdata = internet.document 
Set internetlink = internetdata.getElementsByTagName(tagname) 
For Each internetinnerlink In internetlink 
Sheets("Sheet1").Select 
Sheets(1).Cells(linkcount, 1) = internetinnerlink.href' true 
Sheets(1).Cells(linkcount, 2) = internetinnerlink.innerhtml' true 
Sheets(1).Cells(linkcount, 3) =internetinnerlink.src ' error 
Sheets(1).Cells(linkcount, 4) = internetinnerlink.alt' error 
linkcount = linkcount + 1 
Next internetinnerlink 
End Function` 
+0

什麼恰恰是問題或問題? – Vegard

+0

我不知道如何在innerhtml元素中的元素:( –

+0

只有2行註釋錯誤,這是問題?這些行的預期結果是什麼?可以顯示您將使用的元素類型的示例源代碼這對嗎? – Vegard

回答

0
Function WebPageLinks2(internet, tagname As String, innerlinkcount) 

Dim td As MSHTML.IHTMLElementCollection 
Dim tr As MSHTML.IHTMLElementCollection 
Dim trObj As MSHTML.HTMLGenericElement 
Dim tdObj As MSHTML.HTMLGenericElement 
Dim flag As Boolean 

flag = False 

Set tr = internet.document.getElementsByTagName("a") 
For Each trObj In tr 
    Set td = trObj.getElementsByClassName("graphimage") 
    For Each tdObj In td 
     'do something with each td object' 
     If (InStr(CStr(tdObj.alt), "Agg") <> 0) Or (InStr(CStr(tdObj.alt), "ether1") <> 0) Or (InStr(CStr(tdObj.alt), "Ether1") <> 0) Then 
      Sheets(1).Cells(innerlinkcount, 7) = tdObj.src 
      Sheets(1).Cells(innerlinkcount, 6) = tdObj.alt 
      'Debug.Print tdObj.alt 
      'Debug.Print tdObj.src 
      flag = True 
      Call SaveGraphImageFromURLs(Sheets(5).Cells(17, 3).Value, Sheets(1).Cells(innerlinkcount, 7).Value, _ 
      ToPathOfImages & "\" & Sheets(1).Cells(innerlinkcount, 1).Value & Sheets(1).Cells(innerlinkcount, 3).Value & Sheets(1).Cells(innerlinkcount, 6).Value & ".png", _ 
      Sheets(5).Cells(18, 3).Value, Sheets(5).Cells(19, 3).Value) 
      Sheets(1).Hyperlinks.Add Range("g" & innerlinkcount), hyperlinkAddress 
      Exit For 
     End If 
    Next 
    If InStr(trObj.href, "xport") And flag Then 
     Sheets(1).Cells(innerlinkcount, 5) = trObj.href 
     flag = False 
     Call SaveGraphImageFromURLs(Sheets(5).Cells(17, 3).Value, Sheets(1).Cells(innerlinkcount, 5).Value, _ 
     Sheets(5).Cells(20, 3).Value & Sheets(1).Cells(innerlinkcount, 1).Value & Sheets(1).Cells(innerlinkcount, 3).Value & Sheets(1).Cells(innerlinkcount, 6).Value & ".csv", _ 
     Sheets(5).Cells(18, 3).Value, Sheets(5).Cells(19, 3).Value) 
     Call csvimport((Sheets(5).Cells(20, 3).Value)) 
     Call tahlil(innerlinkcount) 
     Call move_files(CStr((Sheets(5).Cells(20, 3).Value)), CStr(ToPath)) 
     'Debug.Print trObj.href 
     'Debug.Print " \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\" 
     Exit For 
    End If 
Next 

root = root + 1 

End Function