【问题标题】:Getting inner text from HTML options selector using VBA使用 VBA 从 HTML 选项选择器获取内部文本
【发布时间】:2016-08-22 07:40:12
【问题描述】:

我正在尝试从网页的组合框中 (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-50 克罐的价格是 63.95。

这些都是同一产品的不同包装选项。我需要在下表中带入数据:

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

任何见解如何做到这一点?

【问题讨论】:

  • 看起来像是一道作业题。
  • VBA?跑哪儿?你的意思是你在刷屏? stackoverflow.com/questions/27066963/…
  • 您可以使用 IE 自动化来执行此操作:谷歌搜索“VBA 自动化 IE” - 那里有很多示例(以及这里的 SO)
  • VBA 中的 Google Web 控件
  • 是的,它的 VBA 在 excel 中运行。

标签: html vba


【解决方案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

【讨论】:

  • 这是伟大的瑞恩,非常感谢。还有一个问题 - 问题是每个选项都有其价格,我正在努力将所选选项的相关价格引入我的电子表格。价格变化取决于选项,价格在下面的 HTML 代码中:

    £12.94 £63.95

猜你喜欢
  • 2013-08-06
  • 2015-06-29
  • 1970-01-01
  • 2021-11-26
  • 2013-06-12
  • 2013-12-09
  • 2022-07-16
  • 2017-08-10
相关资源
最近更新 更多