简体   繁体   English

使用VBA复制网站数据

[英]Copy website data using VBA

I want to get some data from a webpage, this data contains a 'ClassName'. 我想从网页中获取一些数据,该数据包含“ ClassName”。 The name of the Class is "oem". 该类的名称是“ oem”。 Below here you'll find an example of one page: 在下面,您将找到一页的示例:

<div class="part">
      <h2>HF151<span class="filter-type"> [Oil Filter]</span></h2>
<div class="images">
      <a href="fileadmin/code/images/large/HF151 Oil Filter 2017_03_13-scr.jpg" class="hf-drawing" rel="lightbox" title="">
              <img height="185" src="fileadmin/code/images/small/HF151 Oil Filter 2017_03_13-wtm.jpg"></a>
      <a href="fileadmin/code/images/drawings_large/HF151.png" class="hf-drawing" rel="lightbox" title="">
              <img height="185" src="fileadmin/code/images/drawings_small/HF151.png"></a>     </div>
      <h3>Replaces OEM numbers:</h3>
      <ul class="oems">
      <li class="oem">Aprilia 0256185</li>
      </ul>

      <ul class="oems">
      <li class="oem">BMW 11 41 2 343 118</li>
      </ul>

      <ul class="oems">
      <li class="oem">BMW 11 41 2 343 452</li>
      </ul>

      <ul class="oems">
      <li class="oem">Bombardier 711256185</li>
      </ul>

      <ul class="oems"><
      li class="oem">Husqvarna 7700180</li>
      </ul>

      <div style="clear: both"></div>
      </div>

I want to copy all the 'OEM' data, under each other in an excel sheet. 我想将所有“ OEM”数据互相复制到一个Excel工作表中。 I started with the following code, which is working so far for only 1 row: 我从下面的代码开始,到目前为止,该代码仅适用于1行:

Sub ImportCrossreferenceData()

Dim IE As InternetExplorer
Dim html As HTMLDocument

Set IE = New InternetExplorer
IE.Visible = False
IE.Navigate "http://www.hiflofiltro.com/catalogue/filter/HF151"

'Wait until IE is done loading page
Do While IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop

'Get Data
Set html = IE.document
Set holdingsClass = html.getElementsByClassName("oem")
Range("A1").Value = holdingsClass(0).textContent

'Quit and clean
IE.Quit
Set IE = Nothing

End Sub

I know that if you change the value of the holding class from 0 to 1 or 2, you'll get the second or third value. 我知道,如果将持有类的值从0更改为1或2,则会得到第二个或第三个值。 Unfortunately every page has a different amount of OEM values. 不幸的是,每个页面都有不同数量的OEM值。 I would like that my script count the amount of 'li class="oem"' , and copy these values under eachother in an excel sheet. 我希望我的脚本计算'li class="oem"' ,然后将这些值互相复制到Excel工作表中。

You can get all the elements which belong to the same ClassName in an element collection and then iterate through them. 您可以在element collection获取属于同一ClassName的所有element collection ,然后对其进行迭代。

Please give this a try... 请尝试一下...

Sub ImportCrossreferenceData()

Dim IE As InternetExplorer
Dim html As HTMLDocument
Dim holdingsClasses As IHTMLElementCollection
Dim holdingsClass As IHTMLElement
Dim cell As Range
Dim lr As Long

Set IE = New InternetExplorer
IE.Visible = False
IE.Navigate "http://www.hiflofiltro.com/catalogue/filter/HF151"

'Wait until IE is done loading page
Do While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop

'Get Data
Set html = IE.document
Set holdingsClasses = html.getElementsByClassName("oem")

Set cell = Range("A1")

For Each holdingsClass In holdingsClasses
    cell.Value = holdingsClass.innerText
    Set cell = cell.Offset(1)
Next holdingsClass

lr = Cells(Rows.Count, 1).End(xlUp).Row

'Split column A data into columns using space as delimiter. Delete if not required
Range("A1:A" & lr).TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Space:=True
IE.Quit
Set IE = Nothing

End Sub

Try this. 尝试这个。 It will fetch you all the values you are after. 它将获取您需要的所有值。

Sub Oem_Value()

    Dim post As Object

    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate "http://www.hiflofiltro.com/catalogue/filter/HF151"
        While .readyState < 4: DoEvents: Wend
        For Each post In .document.getElementsByClassName("oem")
            r = r + 1: Cells(r, 1) = post.innerText
        Next post
        .Quit
    End With

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM