【问题标题】:Need to pull details specific details from web page using vba需要使用 vba 从网页中提取详细信息
【发布时间】:2015-07-07 19:41:05
【问题描述】:

我需要使用 VBA 从查看源选项卡中提取文本“目录管理器/销售”和“EOL(产品/组件)”。

下面是查看源码:

<tr>
    <td nowrap="true" valign="top" width="165px" class="ms-formlabel"><h3 class="ms-standardheader"><a name="SPBookmark_Requesting_x0020_Group"></a>Requesting Group</h3></td>
    <td valign="top" class="ms-formbody" width="450px" id="SPFieldChoice">
        <!-- FieldName="Requesting Group"
             FieldInternalName="Requesting_x0020_Group"
             FieldType="SPFieldChoice"
          -->
        Catalog Managers/ Sales
    </td>
</tr>

<tr>
    <td nowrap="true" valign="top" width="165px" class="ms-formlabel"><h3 class="ms-standardheader"><a name="SPBookmark_Reason_x0020_for_x0020_change"></a>Reason for change</h3></td>
    <td valign="top" class="ms-formbody" width="450px" id="SPFieldChoice">
        <!-- FieldName="Reason for change"
             FieldInternalName="Reason_x0020_for_x0020_change"
             FieldType="SPFieldChoice"
          -->
        EOL (product/component)
    </td>
</tr>

有多个id="SPFieldChoice",我只需要提取“请求组”和“更改原因”的详细信息。

我正在编写下面的代码来获取 excel 中的详细信息,但它不是特定于我的要求。

Set hcol = ie.document.getElementsByTagName("td")
    For Each inps In hcol
        If inps.ID = "SPFieldChoice" Then
            Sheets("Sheet2").Range("A" & j).Value = inps.innerText
        End If
    Next

需要一个只能提取上述所需详细信息的代码。

【问题讨论】:

  • 节点是否过多?
  • 我是 vba 新手,但在 google 中研究了节点后,我可以说“是”,代码中有很多节点。使用我上面的代码,我可以在不同的工作表中获取所有文本(例如 EOL(产品/组件)),然后我可以在我的主工作表中获取所需的文本。但这似乎不是让我的宏工作的最佳方式。

标签: html vba excel view-source


【解决方案1】:

这只是一个建议(我无法验证答案),但我想说明您可以尝试什么:

Set hcol = Set hcol = ie.document.getElementsByTagName("td")

For Each inps In hcol
    If inps.ID = "SPFieldChoice" Then
        If InStr(1, inps.innerHTML, "Requesting Group") > 0 Or InStr(1, it, "Reason for change") > 0 Then
            Sheets("Sheet2").Range("A" & j).Value = inps.innerText
        End If
    End If
Next

【讨论】:

  • 上面的代码似乎不起作用。有什么办法可以转到下一个节点。 codeSet hcol = ie.document.getElementsByTagName("td") For Each inps In hcol If inps.innerText = "Requesting Group" Then **'move to next node** 'Sheets("Sheet2").Range("B" &amp; j).Value = inps.innertext j = j + 1 End If Nextcode
  • 它不会拉任何东西
  • 对于 it 变量,它会拉出“目录管理器/销售”,然后它不满足提到的 IF 条件并退出 IF 并搜索下一个 inps.id =“SPFieldChoice”
  • 我做了一些更改,请立即尝试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
  • 2014-08-02
  • 2013-11-07
相关资源
最近更新 更多