Qui il codice
- Codice: Seleziona tutto
Option Explicit
Dim WPage As Object
Sub PrintTables()
Dim Ticker As String, myUrl As String
Dim intest, element As Object
Dim r As Long, c As Long, J As Long
' attivare Selenium Library
Dim url As String
Cells.Clear
'
'Crea Driver:
If WPage Is Nothing Then
Set WPage = CreateObject("Selenium.ChromeDriver")
End If
Sheets("AllTables").Activate
Range("A:M").ClearContents
'
With WPage
.Start "Chrome", ""
.Get "https://www.xxxxxxxxxxx"
'.FindElementByClass("cl-consent__btn").Click
'Delay (2)
.FindElementByXPath("//a[@data-role='b_agree']").Click
Delay (4)
r = 2: c = 1
intest = Array("Nome", "Ultimo prezzo", "Var %", "Ora ult. prezzo", "Volume progr.", _
"Migliore denaro", "Migliore lettera", "Prezzo di rifer.", "Apertura", "Codice ISIN", "MF Risk")
Range("A1:K1") = intest
For J = 1 To 60 ' pagine totali
'Prima pagina:
Call GetAllTablesArr(myUrl, 1, 1) 'Posiziona in colonna A
'Delay (4)
r = Cells(Rows.Count, 1).End(xlUp).Row + 1
On Error Resume Next
WPage.FindElementByCss("body > div.skin-wrap > div.content_flex_wrapper > section.markets.container-fluid > div > div:nth-child(3) > div > nav > ul > li:nth-child(7)").Click
On Error GoTo 0
' Delay (8)
Next J
End With
'Quit Selenium
WPage.Quit
Set WPage = Nothing
MsgBox ("Informazioni raccolte...")
End Sub
Sub GetAllTablesArr(myUrl As String, Optional rNum0 As Long = 1, Optional cNum0 As Long = 1)
Dim TBColl As Object
Dim I As Long, J As Long, myTim As Single
Dim RNum As Long, CNum As Long
Dim TArr
If WPage Is Nothing Then
Set WPage = CreateObject("Selenium.ChromeDriver")
End If
'myUrl = "https://www.milanofinanza.it/quotazioni/ricerca/listino-completo-2ae?refresh_cens"
'WPage.Get myUrl
'
myTim = Timer
'
Set TBColl = WPage.FindElementsByTag("table")
RNum = rNum0: CNum = cNum0
'
For I = 1 To TBColl.Count 'Scan delle Tabelle presenti
TArr = TBColl(I).AsTable.Data
RNum = RNum + 1
Cells(RNum, CNum).Value = "## Table " & I
If (UBound(TArr) * UBound(TArr, 2)) > 0 Then
Cells(RNum + 1, CNum).Resize(UBound(TArr), UBound(TArr, 2)).Value = TArr
End If
RNum = RNum + UBound(TArr) + 1
DoEvents
Next I
Debug.Print "FINE", RNum, Format(Timer - myTim, "0.00"), myUrl
End Sub
Function Delay(Seconds As Long)
Dim StopTime As Date: StopTime = DateAdd("s", Seconds, Now)
Do While Now < StopTime
DoEvents
Loop
End Function