【问题标题】:Extract list of all input boxes on webpage vba提取网页vba上所有输入框的列表
【发布时间】:2018-05-07 06:19:57
【问题描述】:

我想在 Excel 上创建一个网页上所有输入框标签的列表 - 所以我想代码会是这样的:

Sub IEInteract()

Dim i As Long
Dim URL As String
Dim IE As Object
Dim objCollection As Object

Set IE = CreateObject("InternetExplorer.Application")

IE.Visible = True

URL = "mywebsite.com"

IE.Navigate URL

Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop

objCollection = IE.Document.getElementsByTagName("input")

For Each el In objCollection
label = el.label 'or something like that????'
Debug.Print label

Next el

End Sub

我哪里错了?谢谢

顺便说一句,我的 VBA 没问题,但我的 HTML 不存在。

【问题讨论】:

  • 你说所以我想代码会是这样的:不,你无法想象这样。选择一个与您正在使用的网页或多或少相同的网页,然后描述要做什么。这太他妈的宽泛了。
  • @SIM 感谢您告诉我。我正在练习能够概括,但这里有一个示例 mrexcel.com/forum/register.php 。差异在哪里?

标签: vba


【解决方案1】:
  1. 出于学习目的,可以选择具有更明显输入框而不是下拉菜单的网站。
  2. 许多输入框不会被预先填充,因此可以考虑读取检索到的元素的其他属性。甚至写信给他们,然后检索这些值。
  3. 按标签名称选择可以带回许多您可能没有预料到的项目。

牢记以上所有内容。尝试运行以下命令,生成<input> 标签元素的集合。

代码:

Option Explicit

Public Sub PrintTagInfo()
    'Tools > references > Microsoft XML and HTML Object library
    Dim http As New XMLHTTP60                    '<== this will be specific to your excel version
    Dim html As New HTMLDocument

    With http
        .Open "GET", "https://www.mrexcel.com/forum/register.php", False
        .send
        html.body.innerHTML = .responseText
    End With

    Dim inputBoxes As MSHTML.IHTMLElementCollection, iBox As MSHTML.IHTMLElement, i As Long

    Set inputBoxes = html.getElementsByTagName("input") '<== the collection of input tags on the page
    '<== These are input boxes i.e. you are putting info into them so perhaps populate and then try to read what is in the entry box?
    For Each iBox In inputBoxes
        Debug.Print "Result #" & i + 1
        Debug.Print vbNewLine
        Debug.Print "ID: " & iBox.ID '<== select a sample of properties to print out as some maybe empty
        Debug.Print "ClassName: " & iBox.className,
        Debug.Print "Title: " & iBox.Title
        Debug.Print String$(20, Chr$(61))
        Debug.Print vbNewLine
        i = i + 1
    Next iBox
End Sub

示例输出:

从上面看,如果您正在寻找目标框来输入信息,那么类名在某些方面可能会提供更多信息。

对页面源的初步检查、选择输入框并右键单击 > 检查...将帮助您优化选择。

我注意到很多感兴趣的盒子都有Input标签,然后是type = "text"

这意味着您可以使用CSS selectors 定位匹配此模式的元素。在这种情况下使用选择器input[type=""text""]

调整以前的代码以将其考虑在内,从而产生更小、更有针对性的结果。注意,使用.querySelectorAll 来应用CSS 选择器会返回一个NodeList 对象,该对象需要不同的迭代方法。 For Each 循环将导致 Excel 崩溃,如 here 所述。

代码:

Option Explicit
Public Sub PrintTagInfo()
    'Tools > references > Microsoft XML and HTML Object library
    Dim http As New XMLHTTP60                    '<== this will be specific to your excel version
    Dim html As New HTMLDocument

    With http
        .Open "GET", "https://www.mrexcel.com/forum/register.php", False
        .send
        html.body.innerHTML = .responseText
    End With

    Dim inputBoxes As Object, i As Long

    Set inputBoxes = html.querySelectorAll("input[type=""text""]") '<== the collection of text input boxes on page. Returned as a NodeList
    '<== These are input boxes i.e. you are putting info into them so perhaps populate and then try to read what is in the entry box?
    For i = 0 To inputBoxes.Length - 1
        Debug.Print "Result #" & i + 1
        Debug.Print vbNewLine
        Debug.Print "ID: " & inputBoxes.Item(i).ID '<== select a sample of properties to print out as some maybe empty
        Debug.Print "ClassName: " & inputBoxes.Item(i).className,
        Debug.Print "Title: " & inputBoxes.Item(i).Title
        Debug.Print String$(20, Chr$(61))
        Debug.Print vbNewLine
    Next i
End Sub

示例结果:

注意:我已经编辑了间距以更适合图像。

通过 VBE 添加的参考 > 工具 > 参考

最后两个是感兴趣的。底部的将是特定于版本的,如果不使用 Excel 2016,您将需要重新编写 XMLHTTP60,即 XML 6.0 以针对您的 Excel 版本。

【讨论】:

  • 非常有帮助!那么“输入”是“标签名称”吗?另外,您的 XML 和我的:set IE = CreateObject("InternetExplorer.Application") etc 之间有什么区别吗?
  • XML 更快,您正在打开浏览器。输入可能意味着不同的东西,但如果你的目标是 那么是的,它是一种标签。 w3schools.com/tags/tag_input.asp
  • 再次感谢您。我已经能够复制它以打印所有标签的内部文本。是否有“label.for”属性?我无法让它工作。
  • 您可以到这里查看主要信息w3schools.com/tags/default.asp。除此之外,右键单击网页并检查。或者使用 chrome 中的 F12 等开发人员工具。里面有丰富的信息。允许您检查 xpath、css、标签.........
  • 刚刚找到它 - 它是 (htmlFor) 而不是 'for' 本身。这是一个 VBA 问题,而不是 HTML 问题。非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-19
相关资源
最近更新 更多