VBA:从div类中的子div类中提取文本

问题描述:

我有一个搜索页面,其结构如下。在页面上有多达70个searchRecord的实例。我需要提取由asterixes ***表示的文本,并将它们放入Excel中的单独单元格中。所以每个searchrecord都会有一个新的行。VBA:从div类中的子div类中提取文本

<div class="searchRecord"> 
       <div class="thumb"> 
        <a href="**SKU**"> 
         <img src="/product_images/.jpg" alt="**Title**" title="**Title**" border="0" />      </a> 
       </div> 
       <div class="desc"> 
        <h1><a href="**SKU**">**Title**</a></h1> 
        <p>**Category**</p> 
        <div class="clear"></div> 
        <div align="center"> 
         <div style="width:30%"> 
          <style>.bv_rating{ margin:3px 0px 0px 30px; }</style><div id='BVRRInlineRating-5030917094484' class="bv_rating"></div>       </div> 
        </div> 
        <div class="prodPrice"> 
         <div style="padding-top: 6px;"> 
          <div class="priceTxt">**Price1**</div> 
          <div class="priceTxt">**Price2**</div><br /><div class="priceTxt">**Price3**</div><br />       </div> 
        </div> 
        <div class="clear"></div> 
</div> 

我需要提取以下:

SKU

标题(有3个可能的例子)

类别

价格1

Price2

Price3

我已经成功地得到这个工作之前,但只有当主DIV类有一个独特的名字 - 这可以用

完成
Set searchres= oHtml.getElementsByClassName("searchRecord")(0).getElementsByTagName("span") 
i = 0 
For Each oElement In searchres 
Sheets("Sheet1").Range("A" & i + 1) = searchres(i).innerText 
i = i + 1 
Next oElement 
+0

下面是否会成功获取httpojb?我得到否认了''.send'公共职能的getURL(tURL作为字符串)作为字符串 昏暗tmpString作为字符串 随机化 昏暗的URL作为字符串 昏暗的XMLHttpRequest访问作为对象 昏暗HTMLDOC为对象 URL = tURL& “&R =” &INT((99999999 *了Rnd())+ 1) 集的XMLHttpRequest =的CreateObject( “Microsoft.XMLHTTP”) 集HTMLDOC =的CreateObject( “HTMLFILE”) 使用XMLHttpRequest 。开 “GET” ,URL,False .send HTMLdoc.body.innerHTML = .responseText tmpString = .responseText 结束随着 的getURL = tmpString 结束Function' – jquerynewbie

我通常不喜欢在没有测试它的情况下先提供代码,但由于没有提供正式的URL,所以唯一的选择是构造来自HTML提供的短小部分的可用网页。

Dim v As Long, vSKUs As Variant 
Dim iDIV As Long, pp As Long 
Dim oHtml As Object 

'at this point oHtml is the HTML body 
With oHtml 
    If CBool(.getElementsByClassName("searchRecord").Length) Then 
     'there is at least one element with the 'searchRecord' class 
     'make room for all of the array elements 
     ReDim vSKUs(1 To .getElementsByClassName("searchRecord").Length, 1 To 5) 
     For iDIV = 1 To .getElementsByClassName("searchRecord").Length 
      With .getElementsByClassName("searchRecord")(iDIV - 1) 
       'get the SKU from the anchor, otherwise skip to next 
       If CBool(.getElementsBytagName("a").Length) Then 
        With .getElementsBytagName("a")(0) 
         vSKUs(iDIV, 1) = .href 
         'get the title from the anchor's image 
         If CBool(.getElementsBytagName("img").Length) Then 
          vSKUs(iDIV, 2) = .Title 
         End If 
        End With 
        'get up to 3 prices 
        If CBool(.getElementsByClassName("prodPrice").Length) Then 
         For pp = 1 To .getElementsByClassName("prodPrice").Length 
          If pp > 3 Then Exit For 'don't get more than 3 
          vSKUs(iDIV, pp + 2) = .getElementsByClassName("prodPrice")(pp - 1).innertext 
         Next pp 
        End If 
       End If 
      End With 
     Next iDIV 
    End If 
End With 

With Worksheets("Sheet1") 
    .Range("A2").Resize(UBound(vSKUs, 1), UBound(vSKUs, 2)) = vSKUs 
End With 

希望,你可以看到使用.Length存在支票如何通过集合迭代一起工作。请记住,集合中的元素具有基于1的长度,但是基于0的索引;例如如果有与searchRecord的类名6个div元素然后所述集合具有一个.Length而是通过被索引为。 A .Length of 表示集合中没有任何内容。

+0

例URL是'http://uk.webuy.com/search/index.php?page=1&stext=%2A&section=1&sortOn=box_name_asc&refinecat=Xbox+360+Games ' – jquerynewbie