Stellar Blade Un'esclusiva PS5 che sta facendo discutere per l'eccessiva bellezza della protagonista. Vieni a parlarne su Award & Oscar!

Excel Forum Per condividere esperienze su Microsoft Excel

VBA - web scraping

  • Messaggi
  • ABCDEF@Excel
    00 27/04/2021 17:14
    Ciao a tutti, se possibile avrei bisogno di alcune info-aiuti
    Sul sito "AGCOM" si possono digitare numeri di telefono. Se questo numero è "registrarto", viene fornito una risposta.
    Sub Indirizzi_associazione()
        Dim IE As Object
        Dim Doc As Object
        Dim r As Integer
        Dim c As Integer
        Dim HTMLtr As Object
        Dim HTMLTable As Object
        Dim HTMLtd As Object
        Dim HTMLTables  As Object
        Dim myURL As String
        Cells.Clear
        Set IE = CreateObject("InternetExplorer.Application")
        myURL = "https://www.agcom.it/numerazionicallcenter"
        With IE
            .Navigate myURL
            .Visible = True
            Do While .Busy: DoEvents: Loop
            Do While .ReadyState <> 4: DoEvents: Loop
        End With
        Set Doc = IE.Document
        Doc.getElementById("numerotelefono").Value = "0123456789" ' riesco inserirlo
        Doc.getElementById("filtro-submit").Click ' non da errore, mà non fa nulla
        MsgBox Doc.getElementById("num-risultati")
        MsgBox Doc.getElementById("odd")
        MsgBox 3
         Stop
        '....
        '....
        IE.Quit
        Set IE = Nothing
        Set Doc = Nothing
    End Sub

    -----------
    Tento di spiegarmi avvio il codice
    1) Perchè mi appare sempre la maschera (accettare i cookie)
    2) Premendo il bottone, dovrebbe cambiare il Link (non fà nulla)
    3) Dato che cambia il link, non sò come riferirmi alla nuova pagina
    [Modificato da ABCDEF@Excel 27/04/2021 17:24]
  • OFFLINE
    dodo47
    Post: 3.156
    Registrato il: 06/04/2013
    Utente Master
    2010
    00 27/04/2021 19:25
    ciao
    ne so poco
    intanto se non lo hai fatto aggiungi il riferimento vb a:
    Microsoft HTML Object Library

    aggiungi:
    Dim oHTML_Element As IHTMLElement

    poi:
    ........
    Set Doc = IE.Document
    Doc.getElementById("numerotelefono").Value = "060606" ' riesco inserirlo ' è il Comune di Roma

    For Each oHTML_Element In Doc.getElementsByTagName("input")
    If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
    Next

    ?????

    saluti
    Domenico
    Win 10 - Excel 2016
  • ABCDEF@Excel
    00 27/04/2021 20:29
    >>>il riferimento vb a:
    Microsoft HTML Object Library ...era già inserito
    In qualche post, ho letto pure di Microsoft Internet Controls

    Grazie mille, adesso entra nella pagina dei risultati.
    Sai per caso come recuperare "quei" 4 valori "cancellati" nella seconda foto?
    EDIT: Aspetta non dirlo, forse ci sono riuscito
    [Modificato da ABCDEF@Excel 27/04/2021 20:36]
  • ABCDEF@Excel
    00 28/04/2021 12:56
    Ci sono riuscito, ho faticato col ciclo For (mà adesso funziona)
    L'unico "neo" che ci mette 2 minuti per 15 numeri (giusti errati che siano)
    Vedi qualche miglioria possibile (per il tempo, credo siano le pagine HTML che devono andare dalla pagina DOMANDA alla alla pagina RISPOSTA per ogni numero).
    Grazie mille
    Option Explicit
    Sub Telefoni_Agcom()
    Dim Ur As Long, x As Long, c As Long, Num As String
        Dim IE As Object
        Dim Doc As Object
        Dim HTMLtr As Object
        Dim HTMLTable As Object
        Dim HTMLtd As Object
        Dim HTMLTables  As Object
        Dim oHTML_Element As IHTMLElement
        Const myURL As String = "https://www.agcom.it/numerazionicallcenter"
    
        Ur = Range("F" & Rows.Count).End(xlUp).Row
            Set IE = CreateObject("InternetExplorer.Application")
    
    For x = 2 To Ur
        c = 7
        Num = Cells(x, 6)
            With IE
                .Navigate myURL
                '.Visible = True
                Do While .Busy: DoEvents: Loop
                Do While .ReadyState <> 4: DoEvents: Loop
            End With
            Set Doc = IE.Document
    
            Doc.getElementById("numerotelefono").Value = Num
            For Each oHTML_Element In Doc.getElementsByTagName("input")
                If oHTML_Element.Type = "submit" Then oHTML_Element.Click:  Exit For ' circa 10 Each
            Next
            ''''''''''''''''''' Aggiunto perchè mi copiava il valore della ricerca precente
            With IE
                Do While .Busy: DoEvents: Loop
                Do While .ReadyState <> 4: DoEvents: Loop
            End With
            Set Doc = IE.Document
            '''''''''''''''''
            Set HTMLTables = Doc.getElementsByClassName("tab-telefonia-fissa")
                For Each HTMLTable In HTMLTables ' qui salta se la ricerrca non da esito
                    For Each HTMLtr In HTMLTable.getElementsByTagName("tr")
                        For Each HTMLtd In HTMLtr.getElementsByTagName("td")
                            Cells(x, c) = HTMLtd.innerText
                            c = c + 1
                        Next HTMLtd
                    Next HTMLtr
                Next HTMLTable
                If Cells(x, 7) = "" Then Cells(x, 7) = "Non esiste"
    Next
        IE.Quit
        MsgBox "Finito" ', fatto in "
        Set IE = Nothing
        Set Doc = Nothing
    End Sub
  • OFFLINE
    dodo47
    Post: 3.160
    Registrato il: 06/04/2013
    Utente Master
    2010
    00 28/04/2021 17:06
    prova a seguire:

    https://www.forumexcel.it/forum/threads/web-scraping.44903/

    saluti




    Domenico
    Win 10 - Excel 2016