使用VBA从HTML选项选择器获取内部文本

时间:2016-08-21 16:24:05

标签: html vba

我正在尝试从网页的组合框(http://www.havanahouse.co.uk/product/rattrays-marlin-flake-pipe-tobacco-50g)获取每种包装的产品名称和价格。

<h1 itemprop="name" class="product_title entry-title">Rattray&#8217;s Marlin Flake Pipe Tobacco 50g tin</h1>

<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">


  <table class="variations" cellspacing="0">
    <tbody>
      <tr>
        <td class="label">
          <label for="pa_packages">Packing</label>
        </td>
        <td class="value">
          <select id="pa_packages" class="" name="attribute_pa_packages" data-attribute_name="attribute_pa_packages">
            <option value="">Choose an option</option>
            <option value="5-x-50g-tins">5 x 50g Tins</option>
            <option value="50g-tin">50g Tin</option>
          </select><a class="reset_variations" href="#">Clear selection</a> 
        </td>
      </tr>
    </tbody>
  </table>



  <p class="price"><span class="amount">&pound;12.94</span>&ndash;<span class="amount">&pound;63.95</span>
  </p>

每个价格仅适用于1个选项。例如,50克锡的价格为12.94,5-x-50g-tins的价格为63.95。

这些是同一产品的所有不同包装选项。我需要在下表中提供数据:

产品名称,包装选项,价格。

有任何见解怎么做?

1 个答案:

答案 0 :(得分:1)

这是一个示例,它将页面加载到指定的URL,等待它加载,然后显示每个元素的InnerText和Value属性。

Public Sub IE_Test()
    Dim IE       As Object: Set IE = CreateObject("InternetExplorer.Application")
    Dim Elements As Object
    Dim Element  As Object

    With IE
        'Show and navigate to page
        .Visible = True
        .Navigate ("http://www.havanahouse.co.uk/product/rattrays-marlin-flake-pipe-tobacco-50g")

        'Wait until page loads
        Do While .busy And .readystate <> 4
            DoEvents
        Loop

        'Create a collection of Option Tags, which are part of pa_packages
        Set Elements = .document.getelementbyID("pa_packages").getelementsbyTagName("option")

        'Show the element's properties
        For Each Element In Elements
            Debug.Print "The InnerText is: " & Element.innertext
            Debug.Print "The Value is: " & Element.Value
        Next
    End With
End Sub