【问题标题】:Get value from web document input element with VBA使用 VBA 从 Web 文档输入元素中获取值
【发布时间】:2014-12-23 15:12:03
【问题描述】:

我很难从名为 points 的输入中检索值 300

这是我的 HTML 和 VBA 代码。

HTML

<td id="myPower_val_9" style="visibility: visible;">
    <input type="text" disabled="disabled" value="300" name="points"></input>
</td>

VBA

Dim ie As Object
Dim myPoints As String

Set ie = CreateObject("InternetExplorer.Application")

With ie
  .Visible = 0
  .navigate "www.example.com"

   While .Busy Or .readyState <> 4 
   DoEvents
   Wend

End With

Dim Doc As HTMLDocument
Set Doc = ie.document

myPoints = Trim(Doc.getElementsByTagName("td")(0).getElementById("myPoints").innerText)
Range("A1").Value = myPoints 

【问题讨论】:

  • 你为什么希望它起作用?
  • 我的工作场所也有类似的html代码。我需要从输入类型中检索值,但我尝试了很多次都没有成功。
  • innerText 将为您提供标签之间的内容;那不是你想要的。您想要“值”属性的值。
  • 你是说如果我把“innerText”改成“value”就能解决问题?
  • 您可能需要将其更改为 .getAttribute("value") 或类似的名称。

标签: html vba excel web-scraping


【解决方案1】:

HTML 代码

我会尝试在 Web 浏览器中编写在 javascript 中操作文档对象模型 (DOM) 的代码,以便您可以使用更好的基于 Web 的调试工具。

这里有几个问题,控制台或调试器可以帮助解决:

  • 您想要获取元素 ID myPoints,但在 HTML 中它只是称为 points
  • 您想通过 ID 获取元素,但您只设置了 name 属性 -
  • 只要name 对元素是唯一的,就不需要先搜索td
  • &lt;input&gt;&lt;/input&gt; 可以看出,输入元素没有innerText&gt;&lt; 内的文本)。相反,他们有一个value 属性
  • 元素通过对象本身的属性公开其属性和其他数据。所以你可以通过查看.value来检查输入的值

这是您尝试执行的操作的 javascript 示例:

var value = document.getElementsByName("points")[0].value;
console.log(value);
<input type="text" disabled="disabled" value="300" name="points" />
    

打开控制台(F12),你应该会看到300

VBA

要将其转换为 Excel 的 VBA 代码,只需确保将括号 () 用于 VB 数组,而不是方括号 [] 用于 JS 数组:

myPoints = Trim(Doc.getElementsByName("points")(0).Value)

应该没问题。

参考文献

由于我不确定您在 VB 中失败的时间点,因此还要确保您的 VBA 脚本中包含所有正确的 Web 引用。

转到工具 > 参考 > 并添加“Microsoft HTML 对象库”和“Microsoft Internet 控件”:

演示

我创建了一个demo in plunker,这样就可以使用一个实时站点而不是 example.com。

将以下代码粘贴到 excel 中,一切正常:

Public Sub GetValueFromBrowser()
    Dim ie As Object
    Dim url As String
    Dim myPoints As String

    url = "http://run.plnkr.co/plunks/6UTb9kHRZ363Ivhh2BPE/"
    Set ie = CreateObject("InternetExplorer.Application")

    With ie
      .Visible = 0
      .navigate url
       While .Busy Or .readyState <> 4
         DoEvents
       Wend
    End With

    Dim Doc As HTMLDocument
    Set Doc = ie.document

    myPoints = Trim(Doc.getElementsByName("points")(0).Value)
    Range("A1").Value = myPoints

End Sub

输出

【讨论】:

    【解决方案2】:

    CSS 选择器:

    使用CSS选择器获取input[name='points']的元素

    您没有显示足够的 HTML 来了解页面上是否只有这一个。上面说带有input标签的元素具有name属性,其值为'points'


    CSS 查询:


    VBA:

    您将 CSS 选择器与 document.querySelector 方法应用于单个元素; .querySelectorAll 表示所有匹配元素的nodeList,即如果页面上有多个元素并且您通过索引获得了感兴趣的元素。

    Debug.Print ie.document.querySelector("input[name='points']").getAttribute("value")
    

    【讨论】:

      【解决方案3】:

      您需要使用.getAttribute("name of attribute") 来获取属性值。在您的情况下,.getAttribute("value") 将返回 300。

      Dim ie As Object
      Dim myPoints As String
      
      Set ie = CreateObject("InternetExplorer.Application")
      
      With ie
        .Visible = 1
        .navigate "website URL"
      
         While .Busy Or .readyState <> 4
         DoEvents
         Wend
      
      End With
      
      Dim Doc As HTMLDocument
      Set Doc = ie.document
      
      myPoints = Trim(Doc.getElementsByTagName("td")(0).getElementsByTagName("input")(0).getAttribute("value"))
      Range("A1").Value = myPoints
      

      只是一个旁注。我对 HTML 了解不多,也许有人可以详细说明一下。但是如果你想测试那个 HTML 代码,你需要在

      标签中添加。
      像这样:
      <table>
          <tr>
              <td id="myPower_val_9" style="visibility: visible;">
                  <input type="text" disabled="disabled" value="300" name="points"></input>
              </td>
          </tr>
      </table>
      

      【讨论】:

        猜你喜欢
        • 2022-01-01
        • 1970-01-01
        • 2019-05-24
        • 1970-01-01
        • 2012-12-29
        • 2022-09-29
        • 1970-01-01
        • 1970-01-01
        • 2017-10-18
        相关资源
        最近更新 更多