需要使用VBA获取Excel单元格行中的所有ID

时间:2017-08-17 15:18:17

标签: vba web-scraping

任何人都可以帮助我:

我需要将所有ID值放入带有VBA的Excel文件的Excel文件中。 我已经获得了TD值,但ID不起作用。

With IE
    .Visible = True
    .navigate ("http://www.")

End With

While IE.Busy: DoEvents: Wend ' Wait for page load

'Clearing any unnecessary or old data in Sheet1
Sheets("Sheet1").Range("A1:B1000").ClearContents

Set TR_Elements = IE.Document.getElementsByTagName("tr")
RowNumb = 2
ColumnNumb = 1
'Tables usually consists of TR (Table Rows), and -
'TD (Table Data)
For Each TR In TR_Elements
    Set TD_Elements = TR.getElementsByTagName("td")
    ColumnNumb = 1
    For Each TD In TD_Elements
        ActiveSheet.Cells(RowNumb, ColumnNumb).Value = TD.innerText
        ColumnNumb = ColumnNumb + 1
    Next
    RowNumb = RowNumb + 1
Next


Set ID_Elements = IE.Document.getElementById("itemnummer")

 r = 0
 For Each ID_ELement In ID_Elements
 Sheets(Sheet1).Range("D1").Offset(r, 1).Value = ID_ELement.Value
    r = r + 1
    Next

以下是网站上的HTML代码:

<li id="product">
    <div id="product">
            <div id="image" align="center"><a href="/product/Olieverf/CH111.html">

        <img src="/shop/thumb/CH111.jpg" border="0" />
        </a></div>

        <div id="titel_text"><a href="/product/Olieverf/CH111.html">Olieverfschilderij + Lijst 100x100cm<br>            </a></div>
        <div id="itemnummer">CH111</div>
        <div id="afmetingen"><table><tr><td>100 x 100 x 4</td><td>Voorraad: 22</td></tr></table><br>
        </div>

        </div>
</li>

1 个答案:

答案 0 :(得分:0)

尝试从

迭代集合

document.querySelectorAll("div#itemnumber")