lundi 18 avril 2016

Web scraping with VBA - Loop over dropdown list (elections data)

I'm new with this web scraping thing, hope you can help me out on this one.

I want to extract all "TD" elements from a webpage looping over all the options values that cdgoDist (dropdown list) has inside cdgoAmbito="P",cdgoDep=="010000" and cdgoProv=="010100" but I'm not sure how to do it

Many thanks in advance

    Sub ONPE()

Dim IE As Object
Dim doc As HTMLDocument
Dim amb As HTMLSelectElement
Dim dep As HTMLSelectElement
Dim prv As HTMLSelectElement
Dim dis As HTMLSelectElement

Set IE = CreateObject("InternetExplorer.Application")

With IE
.Visible = True
.navigate "http://ift.tt/1rc3m5i"
While .Busy Or .readyState <> 4: DoEvents: Wend
Set doc = .document
End With

With doc

Set amb = .getElementById("cdgoAmbito")
amb.Value = "P"
amb.FireEvent "onchange"
Application.Wait DateAdd("s", 2, Now)

Set dep = .getElementById("cdgoDep")
dep.Value = "010000"
dep.FireEvent "onchange"
Application.Wait DateAdd("s", 2, Now)

Set prv = .getElementById("cdgoProv")
prv.Value = "010100"
prv.FireEvent "onchange"
Application.Wait DateAdd("s", 2, Now)

Set dis = .getElementById("cdgoDist")

Dim ele As Object
For Each ele In dis
 If ele.getAttribute("Value") = "???" Then
    ele.Click

    Set elem = .GetElementsByTagName("TD")
    For t = 0 To (elem.Length - 1)
    ThisWorkbook.Worksheets(1).Cells(???, t + 2) = elem(t).innerText
    Next t

    Exit For
 End If
Next ele



End With
End Sub




Aucun commentaire:

Enregistrer un commentaire