Condividi:        

macro GetWebTab2 non funziona con l'url che inserisco.

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

macro GetWebTab2 non funziona con l'url che inserisco.

Postdi gioninos » 25/09/16 22:43

dopo tanto cercare, ho trovato questa fantastica macro, in questo fantastico forum, ma con l'url che inserisco non funziona, perché
Codice: Seleziona tutto
Sub GetWebTab2()
Dim IE As Object, F As Long
Dim myRColl, myDColl, KK As Long, I As Long, J As Long, myColl, myR, myTD
'
myURL = "http://www.oddsportal.com/predictions-archive/#sport/soccer/page/4/"     '<<<<
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 foglio5 (su un nuovo foglio)
'Worksheets.Add                                              '<<<<<1  -Nuovo foglio
Application.Goto (Sheets("Foglio2").Range("A1"))            '<<<<<2  -Foglio esistente
Cells.Clear
Set myColl = IE.Document.getElementsbyTagName("TABLE")
For Each myItm In myColl
    Cells(I + 1, "A").Value = "TABELLA_" & KK: KK = KK + 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
    Next trtr
I = I + 2
Next myItm
'Legge le tabelle dentro gli iframe:
Set myColl = IE.Document.getElementsbyTagName("iframe")
For F = 0 To myColl.Length - 1
If Left(myColl(F).ID, 7) = "myframe" Then
Set my2coll = myColl(F).contentDocument.getElementsbyTagName("table")
    For Each myItm In my2coll
    Cells(I + 1, "A").Value = "TABELLA_" & KK: KK = KK + 1: I = I + 1
        Set myRColl = myItm.getElementsbyTagName("tr")
        For Each myR In myRColl
            Set myDColl = myR.getElementsbyTagName("td")
            For Each myTD In myDColl
                Cells(I + 1, J + 1) = myTD.innertext
                J = J + 1
            Next myTD
            I = I + 1: J = 0
        Next myR
    I = I + 2
    Next myItm
End If
Next F
'
    Cells.WrapText = False
    Range("A1").Select
'
'Stop     'Vedi testo
'
'Chiusura IE
IE.Quit
Set IE = Nothing
End Sub
gioninos
Utente Junior
 
Post: 16
Iscritto il: 09/05/16 22:23

Sponsor
 

Re: macro GetWebTab2 non funziona con l'url che inserisco.

Postdi Anthony47 » 26/09/16 01:45

Quel sito impiega una eternita' a stabilizzare il documento anche dopo che readyState abbia raggiunto il valore 4 (che da specifiche Microsoft significa "il Documento ora e' pronto"); probabilmente deriva da cosa fanno i javascrip al suo interno.
Allunga l'attesa aggiuntiva:
non If Timer > myStart + 2 Or Timer < myStart Then Exit Do ma
Codice: Seleziona tutto
    If Timer > myStart + 8 Or Timer < myStart Then Exit Do


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

Re: macro GetWebTab2 non funziona con l'url che inserisco.

Postdi gioninos » 26/09/16 07:13

grazie innanzi tutto per la risposta super veloce, ma il problema continua, quando lancio la macro, invece di scaricarmi i dati della pag http://www.oddsportal.com/predictions-archive/#sport/soccer/page/4/, continua a scaricarmi i dati della pag http://www.oddsportal.com/predictions-archive/#sport/soccer/page/1/, sembra che abbia risolto, allungando ancora il tempo e inserendo
Codice: Seleziona tutto
If Timer > myStart + 30 Or Timer < myStart Then Exit Do
, in questo modo, quando lancio la macro, e si apre il browser ho il tempo di inserire, il link giusto,direttamente ne browser, ma mi sembra una soluzione poco pratica e molto spartana.
gioninos
Utente Junior
 
Post: 16
Iscritto il: 09/05/16 22:23

Re: macro GetWebTab2 non funziona con l'url che inserisco.

Postdi Anthony47 » 26/09/16 22:24

Se mi dici solo che "non funziona" io mi limito solo alle cose evidenti...
Comunque la macro importa la pagina che si apre con quell'url (controlla a mano e vedrai che ambedue i link del tuo messaggio precedente aprono la stessa pagina).
Pero' ho notato che se ripeto la navigazione allora la pagina si apre correttamente; quindi modifica la parte iniziale della macro in
Codice: Seleziona tutto
reDo:
    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 + 8 Or Timer < myStart Then Exit Do
    Loop
    If re1 = 0 Then
        re1 = 1: GoTo reDo
    End If
    re1 = 0
    'Leggi le tabelle su foglio5 (su un nuovo foglio)

Questo in realta' forzera' due navigazioni all'url specificato, la seconda va alla pagina impostata.

Guarda inoltre che nella macro che stai usando la parte di codice tra "Legge le tabelle dentro gli iframe" (inclusa) e Next F (inclusa) e' superflua (nel tuo contesto), quindi lo puoi eliminare.

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

Re: macro GetWebTab2 non funziona con l'url che inserisco.

Postdi gioninos » 27/09/16 14:42

grazie di nuovo per la risposta iper veloce ed efficace, se riesci a risolvermi questi due problemi, non disturbo più. forse :D
1 problema, quando scarico i dati mi da i valori nelle celle A4, B4, C4 in valore doppio, a me servirebbero in valore singolo, no ad es 4%4%. Quindi,non essendo un grande esperto di vba, col registratore ho provato a cambiarli in questo modo:
Codice: Seleziona tutto
Sub CORREGGIPERC()
'
' CORREGGIPERC Macro
'

'
    Range("A4").Select
    ActiveCell.FormulaR1C1 = "94%"
    Range("B4").Select
    ActiveCell.FormulaR1C1 = "0%"
    Range("C4").Select
    ActiveCell.FormulaR1C1 = "6%"
    Range("A10").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "93%"
    Range("B10").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "4%"
    Range("C10").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "4%"
    Range("A16").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "94%"
    Range("B16").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "5%"
    Range("C16").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "1%"
    Range("A22").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "5%"
    Range("B22").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "2%"
    Range("C22").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "94%"
    Range("A28").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "57%"
    Range("B28").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "17%"
    Range("C28").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "27%"
    Range("A34").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "94%"
    Range("B34").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "3%"
    Range("C34").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "2%"
    Range("B34").Select
    ActiveWindow.SmallScroll Down:=12
    Range("A40").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "43%"
    Range("B40").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "25%"
    Range("C40").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "32%"
    Range("A46").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "14%"
    Range("B46").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "11%"
    Range("C46").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "75%"
    Range("A52").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "6%"
    Range("B52").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "3%"
    Range("C52").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "90%"
    Range("A58").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "" & Chr(13) & "98%"
    Range("B58").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "" & Chr(13) & "1%"
    Range("C58").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "" & Chr(13) & "1%"
    Range("A64").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "89%"
    Range("B64").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "4%"
    Range("C64").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "6%"
    Range("A70").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "" & Chr(13) & "86%"
    Range("B70").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "5%"
    Range("B70").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "" & Chr(13) & "5%"
    Range("C70").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "9%"
    Range("A76").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "97%"
    Range("B76").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "1%"
    Range("C76").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "2%"
    Range("A82").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "16%"
    Range("B82").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "14%"
    Range("C82").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "70%"
    Range("A88").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "87%"
    Range("B88").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "2%"
    Range("C88").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "11%"
    Range("A94").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "3%"
    Range("B94").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "97%"
    Range("A100").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "97%"
    Range("B100").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "3%"
    Range("A106").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "91%"
    Range("B106").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "4%"
    Range("C106").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "6%"
    Range("A112").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "86%"
    Range("B112").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "3%"
    Range("C112").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "11%"
    Range("A118").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "" & Chr(13) & "79%"
    Range("B118").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "" & Chr(13) & "13%"
    Range("C118").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "" & Chr(13) & "8%"
    Range("A124").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "92%"
    Range("B124").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "4%"
    Range("C124").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "4%"
    Range("A130").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "93%" & Chr(13) & "" & Chr(10) & "" & Chr(13) & ""
    Range("B130").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "1%" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "1%"
    Range("B130").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "1%"
    Range("A130").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "93%" & Chr(13) & ""
    Range("C130").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "6%"
    Range("A136").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "9%"
    Range("B136").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "2%"
    Range("C136").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "89%"
    Range("A142").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "5%"
    Range("B142").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "5%"
    Range("C142").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "" & Chr(13) & "90%"
    Range("A148").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "" & Chr(13) & "14%"
    Range("B148").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "" & Chr(13) & "0%"
    Range("C148").Select
    ActiveCell.FormulaR1C1 = "" & Chr(13) & "" & Chr(13) & "86%"
    Range("A151").Select
    ActiveWindow.SmallScroll Down:=-159
    Range("A151:I186").Select
    Selection.ClearContents
    ActiveWindow.SmallScroll Down:=-21
End Sub
,ma è una gran caxata,perche quando scarico i nuovi dati e correggo con questa macro, mi inserisce le vecchie perc.

quesito 2
è possibile scaricare anche i dati che spuntano a tendina sul sito, quando mi avvicino alle percentuali https://www.dropbox.com/home?preview=Immagine.png
gioninos
Utente Junior
 
Post: 16
Iscritto il: 09/05/16 22:23

Re: macro GetWebTab2 non funziona con l'url che inserisco.

Postdi Anthony47 » 27/09/16 23:03

Il fatto e' che nel sorgente c'e' proprio scritto (mettiamo) 6% 6%; la macro si pone l'obiettivo di importare i dati di tabella come sono, le elaborazioni successive dovresti farle tu.
Comunque puoi sostituire il blocco
For Each tdtd In trtr.Cells
Cells(I + 1, J + 1) = tdtd.innertext
J = J + 1
Next tdtd
Con
Codice: Seleziona tutto
            For Each tdtd In trtr.Cells
                If (Len(tdtd.innertext) - Len(Replace(tdtd.innertext, "%", "", , , vbTextCompare))) = 2 Then
                    Cells(I + 1, J + 1) = Replace(Replace(Left(tdtd.innertext, InStr(1, tdtd.innertext, "%", vbTextCompare)), Chr(10), "", , , vbBinaryCompare), Chr(13), "", , , vbBinaryCompare)
                Else
                    Cells(I + 1, J + 1) = tdtd.innertext
                End If
                J = J + 1
            Next tdtd

Per quanto riguarda la richiesta di "scaricare anche i dati che spuntano a tendina sul sito, quando mi avvicino alle percentuali", il link dropbox porta alla home page del mio dropbox quindi non e' utile; se intendi le informazioni che compaiono quando passi il mouse sopra alcuni campi allora la risposta e' "non lo so fare"; quei dati infatti compaiono per effetto di javascript associati all'evento "MouseOver"/"MouseOut", e non sono presenti nel sorgente html.

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

Re: macro GetWebTab2 non funziona con l'url che inserisco.

Postdi gioninos » 28/09/16 11:28

semplicemente favoloso, peccato per il secondo quesito, continuerò a caricarli 1 per 1, ma già è un notevole passo avanti

grazie
gioninos
Utente Junior
 
Post: 16
Iscritto il: 09/05/16 22:23


Torna a Applicazioni Office Windows


Topic correlati a "macro GetWebTab2 non funziona con l'url che inserisco.":


Chi c’è in linea

Visitano il forum: Nessuno e 59 ospiti