Condividi:        

Web Query - Problema con script

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

Web Query - Problema con script

Postdi peterparker0607 » 09/09/16 22:02

Ciao a tutti, sono nuovo del forum.

Scrivo perchè da oggi ho un problema su una macro con web query che fino a poco tempo fa non mi dava problemi.
L'URL dal quale voglio scaricare i dati è questo: http://www.eurocupbasketball.com/eurocu ... code=U2015
Scaricando i dati con la macro sottostante, non solo venivano scaricate le tabelle che vedete (della sezione Boxscore), ma anche quella che si vede nella sezione Play By Play, che in teoria hanno questo URL: http://www.eurocupbasketball.com/eurocu ... playbyplay , ma che in realtà non serve specificare.
Ecco il codice con cui scaricavo tutto.


Codice: Seleziona tutto
Sub Scarica()
 
    With ActiveSheet.QueryTables.Add(Connection:= _
            "URL;http://www.eurocupbasketball.com/eurocup/games/results/showgame?gamecode=301&seasoncode=U2015", Destination:=Range("$A$1"))
        .FieldNames = True
       .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = False
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingRTF
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = True
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
 
End Sub


Facendo girare questo codice, la tabella presente nella sezione play by play non viene scaricata. In effetti, mentre con IE questa tabella la vedo tranquillamente, se apro da Excel->Dati->DaWeb l'indirizzo specificato, da quel browser che mi si apre per scaricare in maniera guidata i dati, la tabella non si carica (prima si caricava).

Cercando un po' su questo forum (si, si, lo so, grazie, no no non c'è bisogno, davvero...) ho trovato questo codice di Antony, che però non risolve il problema putroppo



Codice: Seleziona tutto
Sub GetTabs()
'
myURL = "http://www.eurocupbasketball.com/eurocup/games/results/showgame?gamecode=301&seasoncode=U2015#!playbyplay"
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
 
'Leggi le tabelle, su un  foglio
'Worksheets.Add             'NUOVO
Application.Goto (Sheets("Foglio5").Range("A1"))    '<<<< Foglio specifico
Cells.Clear
Set myColl = IE.document.getElementsByTagName("TABLE")
For Each myItm In myColl      '***1
'Set myitm = mycoll(4)        '***2
    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
    Next trtr
i = i + 1
Next myItm                                             '***1
 
'Chiusura IE
IE.Quit
Set IE = Nothing
End Sub



Che dire... ragazzi, qualcuno riesce a darmi una mano?
Grazie!
peterparker0607
Utente Junior
 
Post: 10
Iscritto il: 09/09/16 10:06

Sponsor
 

Re: Web Query - Problema con script

Postdi peterparker0607 » 12/09/16 10:48

C'è qualcosa che non è chiaro, che potrei specificare meglio?
O davvero non c'è soluzione al problema?
peterparker0607
Utente Junior
 
Post: 10
Iscritto il: 09/09/16 10:06

Re: Web Query - Problema con script

Postdi scossa » 12/09/16 13:13

peterparker0607 ha scritto:C'è qualcosa che non è chiaro, che potrei specificare meglio?


Sì, potresti allegare il file.
Bye!
scossa

Se tu hai una mela, e io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw)
Avatar utente
scossa
Utente Senior
 
Post: 427
Iscritto il: 01/04/12 16:40
Località: Provincia di Verona

Re: Web Query - Problema con script

Postdi Anthony47 » 12/09/16 14:38

Evidentemente il sito ha modificato il modo in cui genera le pagine, passandoo a una modalita' dinamica che poco si presta a essere intercettata tramite webquery.
Anche l'uso "semplificato" della Sub GetTabs non risolve perche' il tuo url integra dati provenienti da url diversi usando la tecnica degli IFrame.
Devi quindi leggere, nel sorgente della pagina iniziale quali sono questi url embedded nella finestra e usarli per estrarre le loro tabelle.
Da quello che vedo, le tabelle PlayByPlay provengono da questo url:
Codice: Seleziona tutto
http://live.euroleague.net/playbyplay?gamecode=301&seasoncode=U2015

A questo punto, invece della Sub GetTabs, ti potra' tornare utile la Sub GetTabbbSub(ByVal myURL As String), una variante che viene richiamata passandogli la stringa Url e usata ad esempio in questa discussione: viewtopic.php?p=619660#p619587
Poi richiamerai in sequenza la Sub GetTabbbSub passandogli gli url da cui vuoi estrarre. Ad esempio:
Codice: Seleziona tutto
    Sheets("Foglio1").Select
    Cells.ClearContents
    Call GetTabbbSub("http://www.eurocupbasketball.com/eurocup/games/results/showgame?gamecode=301&seasoncode=U2015#!playbyplay")
    Sheets("Foglio2").Select
    Cells.ClearContents
    Call GetTabbbSub("http://live.euroleague.net/playbyplay?gamecode=301&seasoncode=U2015")


I sorgenti dei dati presenti negli iFrames sono identificati dall'indicazione data-src="http etc etc"
Questo nel caso che in futuro l'organizzazione del sito cambi nuovamente.

Da quello che vedo ci sono altri 2 iframe non pubblicitari, aventi questi url:
http://live.euroleague.net/shootingchar ... code=U2015
http://live.euroleague.net/graphic?game ... code=U2015

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

Re: Web Query - Problema con script

Postdi peterparker0607 » 12/09/16 16:11

Ho provato il codice che mi hai postato, unito a quello della discussione che hai messo e.... un brivido mi è corso lungo la schiena... it works! Incredibile, grazie!
Ho però un problema. Intanto metto il codice completo, così da agevolare chi dovesse passare di qui in futuro...

Codice: Seleziona tutto
Sub Dati()

    Sheets("Foglio1").Select
    Cells.ClearContents
    Call GetTabbbSub("http://www.eurocupbasketball.com/eurocup/games/results/showgame?gamecode=301&seasoncode=U2015#!playbyplay")
    Sheets("Foglio2").Select
    Cells.ClearContents
    Call GetTabbbSub("http://live.euroleague.net/playbyplay?gamecode=301&seasoncode=U2015")

End Sub

Sub GetTabbbSub(ByVal myURL As String)

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

Set mycoll = ie.document.getElementsByTagName("TABLE")
For Each myItm In mycoll
    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
    Next trtr
i = i + 1
Next myItm

ie.Quit
Set ie = Nothing
End Sub


Il problema è che nell'url dal quale provengono le tabelle, che come hai giustamente indicato è:
http://live.euroleague.net/playbyplay?g ... code=U2015 , ci sono come vedi in alto i pulsanti 1 quarto, 2 quarto...
Col codice sopra mi si scarica solo il primo quarto e non so come scaricare gli altri. Oppure, ancora meglio, non c'è un modo di scaricarli tutti uno in fila all'altro, come riuscivo a fare prima?
peterparker0607
Utente Junior
 
Post: 10
Iscritto il: 09/09/16 10:06

Re: Web Query - Problema con script

Postdi Anthony47 » 12/09/16 22:56

Questo e' un gioco di pazienza che richiede pero' tempo...
Aggiungi una Sub GetTabbbSub22, varizione della Sub GetTabbbSub, con questo codice:
Codice: Seleziona tutto
    Sub GetTabbbSub22(ByVal myURL As String)

    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

    Set mycoll = ie.document.getelementsbytagname("TABLE")
    With mycoll(0)
        Set myquarts = .getelementsbytagname("TR")(0).getelementsbytagname("A")
    End With
    For ii = 0 To myquarts.Length - 1
    myquarts(ii).Click
    Application.Wait (Now + TimeValue("0:00:04"))
        Set mycoll = ie.document.getelementsbytagname("TABLE")
        For Each myItm In mycoll
            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
            Next trtr
        I = I + 1
        Next myItm
    Next ii

    ie.Quit
    Set ie = Nothing
    End Sub

Poi modifica il codice di lancio:
Codice: Seleziona tutto
    Sub Dati()
        Sheets("Foglio1").Selec
        Cells.ClearContents
        Call GetTabbbSub("http://www.eurocupbasketball.com/eurocup/games/results/showgame?gamecode=301&seasoncode=U2015#!playbyplay")
        Sheets("Foglio2").Select
        Cells.ClearContents
        Call GetTabbbSub22("http://live.euroleague.net/playbyplay?gamecode=301&seasoncode=U2015")
    End Sub

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

Re: Web Query - Problema con script

Postdi peterparker0607 » 15/09/16 18:03

Fantastico... La potenza di internet. E' impressionante vedere come la conoscenza venga così facilmente condivisa e di conseguenza ci sia progresso. Forse qui è progresso nelle piccole cose, ma è il piccolo che porta il grande.

Tornando a bomba, mi sono accorto che purtroppo hanno anche cambiato il formato della tabella e questo mi porterà a notevole lavoro extra...
peterparker0607
Utente Junior
 
Post: 10
Iscritto il: 09/09/16 10:06


Torna a Applicazioni Office Windows


Topic correlati a "Web Query - Problema con script":

problema blocco note
Autore: carlin
Forum: Software Windows
Risposte: 7

Chi c’è in linea

Visitano il forum: Nessuno e 70 ospiti