Condividi:        

Recuperare dati da pagina web

Vuoi potenziare i tuoi documenti Word? Non sai come si fa una macro in Excel? Devi creare una presentazione in PowerPoint?
Oppure sei passato a OpenOffice e non sei sicuro di come lavorare al meglio?

Moderatori: Anthony47, Flash30005

Recuperare dati da pagina web

Postdi serra » 20/01/19 17:48

Basandomi su alcuni suggerimenti passati e dalle ricerche sul forum ho costruito questa macro per recuperare i dati da https://www.cftc.gov/dea/futures/financial_lf.htm
Codice: Seleziona tutto
Sub IMPORTADATI()

myURL = "https://www.cftc.gov/dea/futures/financial_lf.htm"
Set IE = CreateObject("InternetExplorer.Application")

With IE
    .navigate myURL
    .Visible = True
    Do While .Busy: DoEvents: Loop    'Attesa not busy
    Do While .readyState <> 4: DoEvents: Loop 'Attesa documento
End With

myStart = Timer  'attesa addizionale
Do
    DoEvents
    If Timer > myStart + 2 Or Timer < myStart Then Exit Do
Loop

Sheets("IMPORTADATI").Activate
Sheets("IMPORTADATI").Select
Cells.Clear

Set mycoll = IE.document.getElementsByTagName("pre")
mySplit = Split(mycoll(0).innertext, Chr(10), , vbBinaryCompare)
Range("A1").Resize(UBound(mySplit) + 1, 1).Value = Application.WorksheetFunction.Transpose(mySplit)

'Debug.Print objIE.document.getElementsByTagName("p")(4).textContent
             
IE.Quit
Set IE = Nothing
End Sub


La macro funziona e importa i dati, il problema è di come vengono importati, tutti nella colonna A e questo rende molto difficile poi recuperarli (essendo anche stringhe molto lunghe) in quanto salta la normale impaginazione della tabella.
Qualche consiglio di come dovrei fare?
serra
Utente Junior
 
Post: 25
Iscritto il: 11/02/18 09:37

Sponsor
 

Re: Recuperare dati da pagina web

Postdi Anthony47 » 20/01/19 18:48

I dati sul sito non hanno nessuna impostazione tabellare, sembrano una normale stampa su carta con le fincature ottenute tramite caratteri "pipe" (|) e "meno" (-)

Con quell'impostazione la loro importazione non puo' che essere "sofferta"
Tuttavia mi pare che puoi usare i ":" presenti nelle righe di intestazioni come separatori di colonna, applicando il comando di Testo-in-colonna ai dati di colonna A:
Immagine

Avrai cosi' i dati numerici abbastanza in ordine, le intestazioni alquanto incomprensibili.

Ciao
Avatar utente
Anthony47
Moderatore
 
Post: 19196
Iscritto il: 21/03/06 16:03
Località: Ivrea

Re: Recuperare dati da pagina web

Postdi serra » 25/02/19 16:35

Salve, ho ancora lo stesso tipo di problema, sperando che questa volta sia possibile recuperare i dati.
https://derivatives.euronext.com/en/products/index-options/PXA-DPAR?Class_type=0&Class_symbol=&Class_exchange=&ex=&ps=999&md=03-2019%2C04-2019%2C05-2019%2C06-2019%2C09-2019%2C12-2019%2C03-2020%2C06-2020%2C09-2020%2C12-2020%2C12-2021%2C12-2022%2C12-2023

Codice: Seleziona tutto
Sub IMPORTADATI()
myURL = "https://derivatives.euronext.com/en/products/index-options/PXA-DPAR?Class_type=0&Class_symbol=PXA&Class_exchange=DPAR&ex=T&ps=999&md=03-2019%2C04-2019%2C05-2019%2C06-2019%2C09-2019%2C12-2019%2C03-2020%2C06-2020%2C09-2020%2C12-2020%2C12-2021%2C12-2022%2C12-2023"
Set ie = CreateObject("InternetExplorer.Application")

With ie
    .navigate myURL
    .Visible = True
    Do While .Busy: DoEvents: Loop    'Attesa not busy
    Do While .readyState <> 4: DoEvents: Loop 'Attesa documento
End With

myStart = Timer  'attesa addizionale
Do
    DoEvents
    If Timer > myStart + 2 Or Timer < myStart Then Exit Do
Loop

Sheets("Foglio2").Activate
Sheets("Foglio2").Select
Cells.Clear

Set mycoll = ie.document.getElementsByclassname("derivitiveinfo_container")
mySplit = Split(mycoll(0).innertext, Chr(10), , vbBinaryCompare)
Range("A1").Resize(UBound(mySplit) + 1, 1).Value = Application.WorksheetFunction.Transpose(mySplit)
           
ie.Quit
Set ie = Nothing
End Sub

Risultato macro
https://postimg.cc/WhtFnqWs
serra
Utente Junior
 
Post: 25
Iscritto il: 11/02/18 09:37

Re: Recuperare dati da pagina web

Postdi Anthony47 » 26/02/19 01:33

Se vuoi importare le tabelle da un sito web allora devi esaminare e lavorare sul codice html del sito, usando i comandi resi disponibili dalla libreria mshtml.tlb.

Tra le tante discussioni sul tema, la mia proposta e' questo codice:
Codice: Seleziona tutto
Sub CALL1()
    Sheets("Foglio1").Select       '<<< Il foglio su cui si fara' l'importazione
    Cells.ClearContents            'NB: Il fofglio SARA' AZZERATO senza preavviso
    Call GetTabbbSub("https://derivatives.euronext.com/en/products/index-options/PXA-DPAR?Class_type=0&Class_symbol=&Class_exchange=&ex=&ps=999&md=03-2019%2C04-2019%2C05-2019%2C06-2019%2C09-2019%2C12-2019%2C03-2020%2C06-2020%2C09-2020%2C12-2020%2C12-2021%2C12-2022%2C12-2023")
    Cells.WrapText = False
End Sub


Sub GetTabbbSub(ByVal myURL As String)
'Va Chiamata passandogli l'URL da leggere
'myURL = "http://www.nhl.com/stats/team?reportType=game&report=teamsummary&season=20152016&gameType=2&aggregate=1&gameLocation=H"
Set IE = CreateObject("InternetExplorer.Application")
   
With IE
    .navigate myURL
    .Visible = True
''Stop                '*** VEDI Testo
    Do While .Busy: DoEvents: Loop    'Attesa not busy
    Do While .readyState <> 4: DoEvents: Loop 'Attesa documento
End With
'
myStart = Timer  'attesa addizionale
Do
    DoEvents
    If Timer > myStart + 2 Or Timer < myStart Then Exit Do
Loop

'Leggi le tabelle SUL FOGLIO ATTIVO
''Sheets("Classifica Home").Select
''Cells.Clear
Set myColl = IE.document.getElementsByTagName("TABLE")
For Each myItm In myColl
    Cells(I + 1, 1) = "Table# " & ti + 1
    ti = ti + 1: I = I + 1
    For Each trtr In myItm.Rows
        For Each tdtd In trtr.Cells
            Cells(I + 1, j + 1) = tdtd.innerText
            j = j + 1
        Next tdtd
        I = I + 1: j = 0
DoEvents
    Next trtr

I = I + 1
Next myItm
'
'Chiusura IE
IE.Quit
Set IE = Nothing
End Sub


Il tutto si compone della Sub GetTabbbSub, che da un sito web importa sul foglio excel tutte le strutture tabelle presenti; e dalla Sub CALL1, che e' quella che predispone il foglio ricevente e poi chiama la Sub precedente passandogli l'Url "da lavorare"

Il tutto va messo in un Modulo standard del vba; poi all'occorrenza si manda in esecuzione la Sub CALL1

Prova e fai sapere...
Avatar utente
Anthony47
Moderatore
 
Post: 19196
Iscritto il: 21/03/06 16:03
Località: Ivrea

Re: Recuperare dati da pagina web

Postdi serra » 26/02/19 10:07

Funziona perfettamente, grazie mille!
serra
Utente Junior
 
Post: 25
Iscritto il: 11/02/18 09:37

Re: Recuperare dati da pagina web

Postdi serra » 26/02/19 10:20

Anzi mi sono accorto che manca l'intestazione di ogni tabella, che dovrebbe essere racchiusa in ogni
Codice: Seleziona tutto
class="derivitiveinfo_subheader"
come posso fare ad aggiungerla prima di ogni tabella?
serra
Utente Junior
 
Post: 25
Iscritto il: 11/02/18 09:37

Re: Recuperare dati da pagina web

Postdi Anthony47 » 27/02/19 01:42

Se vuoi anche le cosiddette "intestazioni", aggiungi queste righe in questa posizione:
Codice: Seleziona tutto
For Each myItm In myColl
    Cells(I + 1, 1) = "Table# " & ti + 1
    ti = ti + 1: I = I + 1
'>>AGGIUNGERE QUESTE RIGHE:
    On Error Resume Next
    Cells(I + 1, 1) = myItm.parentElement.parentElement.getElementsByTagName("font")(0).innerText
    On Error GoTo 0
    I = I + 1
'<< FINE AGGIUNTA
    For Each trtr In myItm.Rows

Ciao
Avatar utente
Anthony47
Moderatore
 
Post: 19196
Iscritto il: 21/03/06 16:03
Località: Ivrea

Re: Recuperare dati da pagina web

Postdi serra » 28/02/19 00:54

Qua codice che ho modificato per avere intestazione solo sulla prima tabella di quel mese (nel caso in cui qualcosa del genere servisse a qualcuno)
Codice: Seleziona tutto
Set mycoll = ie.document.getElementsByTagName("TABLE")
ti = -1
For Each myItm In mycoll
    Cells(I + 1, 1) = "Table# " & ti + 1
    ti = ti + 1: I = I + 1
    On Error Resume Next
    If ti Mod (2) = 0 Then
    Cells(I + 1, 1) = myItm.parentElement.parentElement.getElementsByClassName("derivitiveinfo_subheader")(1 + Y).innerText
    On Error GoTo 0
    Y = Y + 1
    End If
    I = I + 1
    For Each trtr In myItm.Rows
        For Each tdtd In trtr.Cells
            Cells(I + 1, j + 1) = tdtd.innerText
            j = j + 1
        Next tdtd
        I = I + 1: j = 0
DoEvents
    Next trtr

I = I + 1
Next myItm
serra
Utente Junior
 
Post: 25
Iscritto il: 11/02/18 09:37


Torna a Applicazioni Office Windows


Topic correlati a "Recuperare dati da pagina web":


Chi c’è in linea

Visitano il forum: Nessuno e 58 ospiti

cron