【发布时间】:2019-03-03 21:17:20
【问题描述】:
研究这个的第二天。我只是不明白。网页是公开的: https://register.fca.org.uk/ShPo_FirmDetailsPage?id=001b000000MfF1EAAV 手动我 pgdn x 2 到按钮 [+] 个人,单击它然后 pgdn x 1 到“每页结果”下拉菜单并将其更改为 500。然后将结果复制并粘贴到 excel 中
这是我在此站点上找到的代码“从 Web (VBA) 插入数据时选择下拉列表”,由 QHarr 回答,我试图适应并惨遭失败。我把“帮助”放在我认为应该进行更改的地方,但我只是在猜测
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
所以我已经包含了您的更改并在这里。
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
【问题讨论】:
标签: html excel vba internet-explorer web-scraping