简体   繁体   English

使用excel VBA从运行scrpits的网页中获取数据以显示表数据

[英]using excel VBA to grab data from a web page that runs scrpits to show the table data

Day two of researching this. 研究这第二天。 I'm just not getting it. 我只是不明白。 The web page is public: https://register.fca.org.uk/ShPo_FirmDetailsPage?id=001b000000MfF1EAAV Manually I pgdn x 2 to get to the button [+] Individuals, click it then pgdn x 1 to get to the "results per page" drop down and change it to 500. then copy and paste the results into excel 该网页是公开的: https ://register.fca.org.uk/ShPo_FirmDetailsPage?id = 001b000000MfF1EAAV手动我pgdn x 2进入按钮[+]个人,单击它,然后pgdn x 1进入“结果”每页”下拉列表并将其更改为500。然后将结果复制并粘贴到excel中

this is the code that I found on this site "Selecting a dropdown list when inserting data from web (VBA)" answered by QHarr which I tried to adapt and failed miserably. 这是我在该网站上找到的代码“从Web(VBA)插入数据时选择下拉列表”,由QHarr回答,我试图进行调整,但失败了。 I put "HELP" where I think I should be making the changes but I'm just guessing 我将“ HELP”放在我认为应该进行更改的位置,但我只是在猜测

Public Sub MakeSelectiongGetData()
Dim IE As New InternetExplorer
Const URL = "https://register.fca.org.uk/ShPo_FirmDetailsPage?id=001b000000Mfe5TAAR#ShPo_FirmDetailsPage"
'Const optionText As String = "RDVT11"
Application.ScreenUpdating = False
With IE
    .Visible = True
    .navigate URL

    While .Busy Or .readyState < 4: DoEvents: Wend

    Dim a As Object
    Set a = .document.getElementById("HELP")

    Dim currentOption As Object
    For Each currentOption In a.getElementsByTagName("HELP")
        If InStr(currentOption.innerText, optionText) > 0 Then
            currentOption.Selected = "HELP"
            Exit For
        End If
    Next currentOption
    .document.getElementById("HELP").Click
    While .Busy Or .readyState < 4: DoEvents: Wend

    Dim nTable As HTMLTable

    Do: On Error Resume Next: Set nTable = .document.getElementById("HELP"): On Error GoTo 0: DoEvents: Loop While nTable Is Nothing

    Dim nRow As Object, nCell As Object, r As Long, c As Long

    With ActiveSheet
        Dim nBody As Object
        Set nBody = nTable.getElementsByTagName("tbody")(0).getElementsByTagName("tr")
        .Cells(1, 1) = nBody(0).innerText
        For r = 2 To nBody.Length - 1
            Set nRow = nBody(r)
            For Each nCell In nRow.Cells
                c = c + 1: .Cells(r + 1, c) = nCell.innerText
            Next nCell
            c = 0
      Next r
End With
.Quit
End With
Application.ScreenUpdating = True
End Sub

So I have included your changes and am here. 因此,我已经将您的更改包括在内,并且在这里。

Public Sub MakeSelections()
Dim IE As New InternetExplorer
With IE
    .Visible = True
    .Navigate2 "https://register.fca.org.uk/ShPo_FirmDetailsPage?id=001b000000MfF1EAAV"

    While .Busy Or .readyState < 4: DoEvents: Wend

    .document.querySelector("[href*=FirmIndiv]").Click '<==click the + for indiv
    .document.querySelector("#IndividualSearchResults_length[value='500']").Selected = True
End With

Dim nTable As HTMLTable

Do: On Error Resume Next: Set nTable =IE.document.getElementById("IndividualSearchResults"): On Error GoTo 0: DoEvents: Loop While nTable Is Nothing

Dim nRow As Object, nCell As Object, r As Long, c As Long

With ActiveSheet
    Dim nBody As Object
    Set nBody = nTable.getElementsByTagName("Name")(0) _
                      .getElementsByTagName("ShG1_IRN_c") _
                      .getElementsByTagName("ShGl_IndividualStatus__c") _
                      .getElementsByTagName("ShPo_Registerstatus__c") _
                      .getElementsByTagName("Id") _
                      .getElementsByTagName("RecordTypeId") _
                      .getElementsByTagName("CurrencyIsoCode") _
    .Cells(1, 1) = nBody(0).innerText
    For r = 2 To nBody.Length - 1
        Set nRow = nBody(r)
        For Each nCell In nRow.Cells
            c = c + 1: .Cells(r + 1, c) = nCell.innerText
        Next nCell
        c = 0
    Next r
End With

End Sub

You can use css attribute = value selectors to target the + for individuals and also to make the option selection for 500 您可以使用css attribute = value选择器将单个+作为目标,也可以选择500

 Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub MakeSelections()
    Dim IE As New InternetExplorer
    With IE
        .Visible = True
        .Navigate2 "https://register.fca.org.uk/ShPo_FirmDetailsPage?id=001b000000MfF1EAAV"

        While .Busy Or .readyState < 4: DoEvents: Wend

        .document.querySelector("[href*=FirmIndiv]").Click '<==click the + for indiv
        .document.querySelector("#IndividualSearchResults_length [value='500']").Selected = True

        Dim event_onchange As Object
        Set event_onchange = .document.createEvent("HTMLEvents")
        event_onchange.initEvent "change", True, False

        .document.querySelector("[name=IndividualSearchResults_length]").dispatchEvent event_onchange

        Application.Wait Now + TimeSerial(0, 0, 5)
        Dim clipboard As Object, ws As Worksheet

        Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
        Set ws = ThisWorkbook.Worksheets("Sheet1")
        clipboard.SetText .document.querySelector("#IndividualSearchResults").outerHTML
        clipboard.PutInClipboard
        ws.Cells(1, 1).PasteSpecial
        .Quit
    End With
End Sub

This selector, [href*=FirmIndiv] , is an attribute = value selector with contains (*) modifier. 该选择器[href*=FirmIndiv]是带有=(修饰符)的属性=值选择器。 It looks for the matches for href attributes that contain the substring FirmIndiv in the href value. 它在href值中FirmIndiv包含子字符串FirmIndiv href属性的匹配项。 querySelector all method of HTMLDocument *(ie.Document) will return the first match found. HTMLDocument *(即Document)的querySelector all方法将返回找到的第一个匹配项。

You can see the match here: 您可以在这里查看比赛:

The selector for the option tag element (the parent select tag for result counts contains child option tag elements): option标签元素的选择器(结果计数的父select标签包含子option标签元素):

#IndividualSearchResults_length [value='500']

It uses an id (#) selector to target the div parent, of the parent select element, by its id value IndividualSearchResults_length , then uses a descendant combinator (" ") followed by attribute = value selector to specify the option element with value = 500 . 它使用一个ID(#)选择器通过其ID值IndividualSearchResults_length来定位其父select元素的div父对象,然后使用一个后代组合器 (“”),后跟attribute = value选择器来指定value = 500option元素。

You can see that here: 您可以在这里看到:

在此处输入图片说明


Selenium basic version: 硒基本版:

Option Explicit 
Public Sub MakeChanges()
'VBE > Tools > References > Selenium Type Library
'Download: https://github.com/florentbr/SeleniumBasic/releases/tag/v2.0.9.0
    Dim d As WebDriver
    Set d = New ChromeDriver
    Const url = "https://register.fca.org.uk/ShPo_FirmDetailsPage?id=001b000000MfF1EAAV"

    With d
        .Start "Chrome"
        .get url
        .FindElementByCss("[href*=FirmIndiv]").Click
         .FindElementByCss("[name=IndividualSearchResults_length]").WaitDisplayed True, 10000
         .FindElementByCss("[name=IndividualSearchResults_length]").AsSelect.SelectByValue "500"
        Stop                                     '<==delete me later
        .Quit
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM