使用vba从网页获取数据

时间:2019-04-28 08:41:40

标签: html vba web-scraping

我想用VBA从网页中提取一些数据。 这是一个开票程序,我正在尝试访问数据的第一行,这是产品说明 我不是这些问题的专家,但是我调查了作为数据字段之一的html代码,它看起来像这样:

<td class="">
   <div class="UITextbox length length_maximum-255" data-tag="product_code" data-ui-widget="UITextbox"><span class="field"><input autocomplete="off" id="sales_invoice_line_items_attributes_0_product_code" name="sales_invoice[line_items_attributes][0][product_code]" readonly="readonly" size="30" type="text" /></span></div>
</td>
<td class="">
   <div class="UITextbox presence length length_maximum-200" data-tag="description" data-ui-widget="UITextbox"><span class="field"><input autocomplete="off" id="sales_invoice_line_items_attributes_0_description" name="sales_invoice[line_items_attributes][0][description]" size="30" type="text" /></span></div>
</td>
<td class="hidden numeric">
   <div class="UIHiddenField" data-tag="item_id" data-ui-widget="UIHiddenField"><input autocomplete="off" id="sales_invoice_line_items_attributes_0_item_id" label="false" name="sales_invoice[line_items_attributes][0][item_id]" type="hidden" /></div>
</td>
<td class="numeric">
   <div class="UIDecimal presence numericality numericality_greater_than_or_equal_to-0 ensurenotgreaterthanmaxnumber ensurenotgreaterthanmaxnumber_maximum-99999999" data-tag="quantity" data-ui-widget="UIDecimal"><span class="field"><input class="hidden" name="sales_invoice[line_items_attributes][0][quantity]" type="hidden" value="0.00" /><input autocomplete="off" class="visible" data-scale="2" id="sales_invoice_line_items_attributes_0_quantity" name="" size="30" type="text" value="0,00" /></span></div>
</td>
<td class="">
   <div class="UIDropdown" data-tag="unit_type" data-ui-widget="UIDropdown">
   <span class="field"><select autocomplete="off" id="sales_invoice_line_items_attributes_0_unit_type" name="sales_invoice[line_items_attributes][0][unit_type]">

我尝试了这个尝试,但是没有结果:

Dim desc as string

desc = ie.Document.getElementsByClassName("UITextbox presence length length_maximum-200")(0).innerText

以下代码没有错误,但也没有值。

2 个答案:

答案 0 :(得分:0)

您正在查看的Div中没有​​任何文本:

<div class="UITextbox presence length length_maximum-200" data-tag="description" data-ui-widget="UITextbox">
    <span class="field">
        <input autocomplete="off" id="sales_invoice_line_items_attributes_0_description" name="sales_invoice[line_items_attributes][0][description]" size="30" type="text" />
    </span>
</div>

但是Input具有一个ID,您可以使用该ID从中获取值

desc = ie.Document.getElementByID("sales_invoice_line_items_attributes_0_description").value

答案 1 :(得分:0)

也许这不是最有效的方法,但是我找不到其他方法,所以我只是使用sendkeys模拟添加新记录的按键,因为我无法执行fire事件。 感谢您的帮助Ricardo

      IE.Document.getElementById("sales_invoice_line_items_attributes       _0_description").Value ="LINE 1 DESCRIPTION" 

     SendKeys "-"
     SendKeys "{ENTER}"

   IE.Document.getElementById("sales_invoice_line_items_attributes_1_description").Value ="LINE 2 DESCRIPTION"