【问题标题】:VBA Get HTML Element Info for Changing IdVBA 获取更改 ID 的 HTML 元素信息
【发布时间】:2017-12-27 21:10:10
【问题描述】:

我正在尝试创建一个 excel 网络爬虫,它可以登录到我的公司票务跟踪系统并在工作表上记录某些信息(已分配的潜在客户、项目的所需日期等)。我做得很好,直到我不得不从具有更改 ID 的网站上删除一个字段。

例如,在两个页面上,相同的字段将具有 ID:

  • “cq_widget_CqFilteringSelect_32”
  • “cq_widget_CqFilteringSelect_9”

有人可以指导我如何搜索“IT Lead”值并将其粘贴到 Excel 中吗?

HTML snippet of div

Snippet of actual website

Setup in excel

以下是我目前所拥有的

我在这方面感到困惑:

lead = objCollection(i).Value

Sub CQscrub()

Dim i As Long
Dim objElement As Object
Dim objCollection As Object
Dim objCollection2 As Object
Dim ie As InternetExplorer
Dim html As HTMLDocument
Dim numbers() As String
Dim size As Integer
Dim row As Integer
Dim objLead As Object
Dim objLead2 As Object
Dim lead As String
Dim counter As Integer

size = WorksheetFunction.CountA(Worksheets(1).Columns(1)) - 4
ReDim numbers(size)

For row = 10 To (size + 10)
    numbers(row - 10) = Cells(row, 1).Value
    'Cells(row, 2) = numbers(row - 10)
Next row


Set ie = New InternetExplorer
ie.Height = 1000
ie.Width = 1000
ie.Visible = True
ie.navigate "http://clearquest/cqweb/"

Application.StatusBar = "Loading http://clearquest/cqweb"

Do While ie.Busy
    Application.Wait DateAdd("s", 1, Now)
Loop

Application.StatusBar = "Searching form. Please wait..."
'Had these below as comment
Dim WRnumber1 As String
WRnumber1 = Range("A10").Value
'Range("A6").Value = WRnumber1


Dim iLastRow As Integer
Dim Rng As Range
iLastRow = Cells(Rows.Count, "a").End(xlUp).row 'last row of A

'Set objCollection = ie.document.getElementsByTagName("input") originally here
For counter = 0 To size - 1
    Set objCollection = ie.document.getElementsByTagName("input")
    i = 0
    While i < objCollection.Length
        If objCollection(i).Name = "cqFindRecordString" Then
            objCollection(i).Value = numbers(counter)

        End If
        i = i + 1
    Wend
    '''''''''''''''''' Find Label ''''''''''''''''''''''''''''
    Set objCollection = ie.document.getElementsByTagName("label")
    i = 0
    While i < objCollection.Length
        If objCollection(i).innerText = "IT Lead/Assigned To" Then
            lead = objCollection(i).Value
            'Set objLead = objCollection(i)
        End If
        i = i + 1
    Wend
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            Range("B" & (iLastRow - (size - counter - 1))).Value = lead
    Set objElement = ie.document.getElementById("cqFindRecordButton")
    objElement.Click
    Do While ie.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop
    Application.Wait (Now + TimeValue("0:00:02"))
Next counter

ie.Quit
Set ie = Nothing
Set objElement = Nothing
Set objCollection = Nothing

Application.StatusBar = ""
MsgBox "Done!"

End Sub

注意:网站仅供内部使用

目标:在“IT Lead/Assigned To”字段下选择名称并粘贴到 Excel

谢谢

【问题讨论】:

  • 是需要保留ID还是直接用正则表达式解析?
  • 只需要解析IT Lead下的名字,我不需要保留

标签: html vba excel web-scraping


【解决方案1】:

关于提供的代码,tl;dr.

但是,如果您想要在 HTML sn-p 中提供的划掉部分,以下可能会起作用(我无法测试我无权访问的内容:D) .

有很多不同的方法来抓取一个元素,这个方法你抓取的是类名dijitReset dijitInputField dijitInputContainer的第一个实例。类名并不总是唯一的值,但由于这个类名有些复杂,我觉得在你的情况下它是安全的。

你可以用一行来Set yourObj...,但为了演示的目的,我决定把它拆开。 设置你的 obj 的 1-liner 方法:

Set yourObj = doc.getElementsByClassName("dijitReset dijitInputField dijitInputContainer")(0).getElementsByTagName("input")(1)

代码片段:

Sub getElementFromIE()

    Dim ie As InternetExplorer

    ' ... your above code pulls up webpage ...

    '''''''''''''''''' Find Label ''''''''''''''''''''''''''''
    Dim doc As HTMLDocument, yourObj As Object
    Set doc = ie.document

    ' I assume the class name is unique? If so, just append (0) as I did below
    Set yourObj = doc.getElementsByClassName("dijitReset dijitInputField dijitInputContainer")(0)
    Set yourObj = yourObj.getElementsByTagName("input")(1)
    lead = yourObj.Value

End Sub

Set yourObj = yourObj.getElementsByTagName("input")(1) 上的(1) 的原因是因为在你的类dijitReset... 后面有2 个input 标签。您想要此标签的第二个实例,其中包含您的值;您可能已经知道,您使用的是Base 0,这意味着第二个实例实际上是数字 1。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-22
    • 2015-01-18
    • 2019-01-11
    • 2018-01-16
    • 2012-07-09
    • 2018-05-17
    • 2017-06-16
    • 1970-01-01
    相关资源
    最近更新 更多