very stange to select item in web dropdown and click on "cerca"

sal21

Active Member
Joined
Apr 1, 2002
Messages
291
based this url.


first...

1) select, vista territoriale
2) flag on, "tutti i comuni della provincia selezionata"
3) Select first value in dropdown
4) click on Cerca

in this case the web show the related table of provincia

i need to make the operation for all item are in dropdown.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Sounds like a job for Power Automate, if you have access to that.
 
Upvote 0
Power Automate | Microsoft Power Platform

If you don't have access to this (probably via your employer), you can use an open source alternative such as UI.Vision.
TKS, bro.

i have resolved with code:
VBA Code:
Option Explicit
Sub PROVA()
    
    Dim IE As Object, T As Integer, Title As Object
    Dim oHtml As HTMLDocument, post As Object
    
    Clear_Cache
    
    Set IE = CreateObject("internetexplorer.application")
    
    'CreateObject("Shell.Application").ShellExecute "microsoft-edge:https://demo.istat.it/app/?i=POS&l=it"
    
    With IE
        
        .Visible = True
        .navigate "https://demo.istat.it/app/?i=POS&l=it"
        
        Do While .Busy
            DoEvents
        Loop
        
        Do While .readyState <> 4
            DoEvents
        Loop
        
        'CLICK SU VISTRA TERRITORIALE
        .document.getElementById("tab-1").Click
        
        'CHEKBOX TUTTI I COMUNI
        .document.all("territorio")(3).Checked = True
        
        'SELEZIONO UN ITEM PROVINCIA NEL COMBOBOX
        Set Title = .document.getElementById("province-1")
        
        'CONTO E PRENDO IL NOME DELLE PROVINCE NEL COMBOBOX
        Set oHtml = .document
        For Each post In oHtml.getElementById("province-1").getElementsByTagName("OPTION")
            'Debug.Print T & "-" & post.innerText
            T = T + 1
        Next
        
        Title.selectedIndex = 105
        
        'CLICK CERCA
        .document.getElementById("btnricerca-1").Click
        
    End With
    
    IE.Quit
    Set IE = Nothing
    
End Sub

But why the ie object dont show the table of result!!!!

to be sure i have tested this step by step on chrome, edge and firefox, the table of result is show!
 
Upvote 0
You could do this with Power Query.

Use your browser tools to view the network requests for one of the provinces, for example Agrigento. The browser sends a POST request to https://demo.istat.it/app/RPCCerca.php, with the following form data:

territorio=procom&province=084&hid-i=POS&hid-a=2022&hid-l=it&hid-cat=POS&hid-dati=dati-form-1&hid-tavola=tavola-form-1

The "084" is the province code for Agrigento, which comes from the dropdown (select element):

HTML:
<select class="w3-select" name="province" id="province-1" title="Seleziona la provincia" required="">
<option value="084">Agrigento</option>
<option value="006">Alessandria</option>

In PQ, add a blank query and in the Advanced Editor, replace the query code with:

Power Query:
let
    url = "https://demo.istat.it/app/RPCCerca.php",
    body  = "territorio=procom&province=084&hid-i=POS&hid-a=2022&hid-l=it&hid-cat=POS&hid-dati=dati-form-1&hid-tavola=tavola-form-1",
    Source = Json.Document(Web.Contents(url,[Headers = [#"Content-Type"="application/x-www-form-urlencoded;charset=UTF-8"], Content = Text.ToBinary(body) ] )),
    datatable = Source[datatable],
    #"Converted to Table" = Record.ToTable(datatable),
    Value = #"Converted to Table"{0}[Value],
    #"Converted to Table1" = Table.FromList(Value, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table1", "Column1", {"codistat", "mtot", "ftot", "totmf"}, {"codistat", "mtot", "ftot", "totmf"})
in
    #"Expanded Column1"
Close and Load the query and it returns 43 rows of data to the sheet.

Next, based on that code, create a custom function which accepts a parameter for the province code and invoke it for all the rows in a table of province names and codes.
 
Upvote 0
You could do this with Power Query.

Use your browser tools to view the network requests for one of the provinces, for example Agrigento. The browser sends a POST request to https://demo.istat.it/app/RPCCerca.php, with the following form data:

territorio=procom&province=084&hid-i=POS&hid-a=2022&hid-l=it&hid-cat=POS&hid-dati=dati-form-1&hid-tavola=tavola-form-1

The "084" is the province code for Agrigento, which comes from the dropdown (select element):

HTML:
<select class="w3-select" name="province" id="province-1" title="Seleziona la provincia" required="">
<option value="084">Agrigento</option>
<option value="006">Alessandria</option>

In PQ, add a blank query and in the Advanced Editor, replace the query code with:

Power Query:
let
    url = "https://demo.istat.it/app/RPCCerca.php",
    body  = "territorio=procom&province=084&hid-i=POS&hid-a=2022&hid-l=it&hid-cat=POS&hid-dati=dati-form-1&hid-tavola=tavola-form-1",
    Source = Json.Document(Web.Contents(url,[Headers = [#"Content-Type"="application/x-www-form-urlencoded;charset=UTF-8"], Content = Text.ToBinary(body) ] )),
    datatable = Source[datatable],
    #"Converted to Table" = Record.ToTable(datatable),
    Value = #"Converted to Table"{0}[Value],
    #"Converted to Table1" = Table.FromList(Value, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table1", "Column1", {"codistat", "mtot", "ftot", "totmf"}, {"codistat", "mtot", "ftot", "totmf"})
in
    #"Expanded Column1"
Close and Load the query and it returns 43 rows of data to the sheet.

Next, based on that code, create a custom function which accepts a parameter for the province code and invoke it for all the rows in a table of province names and codes.

WOW!
but not for me to create a function code, can you help me?
 
Upvote 0
WOW!
but not for me to create a function code, can you help me?
Ok, I'll try to guide you through the steps to get the data for multiple provinces. Power Query is relatively new to me so some steps might not be optimum.

1. First, create the following table on a sheet and rename it Provinces.

CodeProvince
084Agrigento
006Alessandria
042Ancona

The Code column is formatted as Text, and I had to type in the leading zeroes to make all the codes 3 digits long. For this guide, the Provinces table contains only 3 provinces but you can easily expand it to all the provinces.

2. Note - you could skip steps 2 and 3 and go directly to step 4 by creating a blank connection-only query named GetProvinceData, but here I will show you all the individual manual steps needed to get to the web data.

Next, create the basic query which sends a POST request for the province with code 084:

Data tab -> Get Data -> Other sources -> Blank Query. In Advanced Editor, replace everything with:

Power Query:
let
    url = "https://demo.istat.it/app/RPCCerca.php",
    body  = "territorio=procom&province=084&hid-i=POS&hid-a=2022&hid-l=it&hid-cat=POS&hid-dati=dati-form-1&hid-tavola=tavola-form-1",
    Source = Json.Document(Web.Contents(url,[Headers = [#"Content-Type"="application/x-www-form-urlencoded;charset=UTF-8"], Content = Text.ToBinary(body) ] ))
in
    Source
Click Done to close Advanced Editor.

3. Now we will drill down to the required data, which is in JSON format.

Click Record next to datatable.
Click List next to data.
Click To Table on the Transform tab. Click OK in the To Table dialogue.
Click the Expand icon at the top of Column1.
Select 5 columns: codistat, denominazione, mtot, ftot, totmf. Untick 'Use original column name as prefix'. Click OK.
Under Properties, change the name of the query from Query1 to GetProvinceData.
Click Advanced Editor to view the PQ code, which should be:

Power Query:
let
    url = "https://demo.istat.it/app/RPCCerca.php",
    body  = "territorio=procom&province=084&hid-i=POS&hid-a=2022&hid-l=it&hid-cat=POS&hid-dati=dati-form-1&hid-tavola=tavola-form-1",
    Source = Json.Document(Web.Contents(url,[Headers = [#"Content-Type"="application/x-www-form-urlencoded;charset=UTF-8"], Content = Text.ToBinary(body) ] )),
    datatable = Source[datatable],
    data = datatable[data],
    #"Converted to Table" = Table.FromList(data, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"codistat", "denominazione", "mtot", "ftot", "totmf"}, {"codistat", "denominazione", "mtot", "ftot", "totmf"})
in
    #"Expanded Column1"

Click Done to close Advanced Editor.
Click Close & Load To...
Important: Click Only Create Connection. This creates a connection-only query named GetProvinceData.

4. Now change the GetProvinceData query to a custom function which accepts a parameter for the province code:

Edit the GetProvinceData query and in Advanced Editor replace everything with:

Power Query:
let GetProvinceData = (ProvinceCode as text) =>
    let
        url = "https://demo.istat.it/app/RPCCerca.php",
        body  = "territorio=procom&province="&ProvinceCode&"&hid-i=POS&hid-a=2022&hid-l=it&hid-cat=POS&hid-dati=dati-form-1&hid-tavola=tavola-form-1",
        Source = Json.Document(Web.Contents(url,[Headers = [#"Content-Type"="application/x-www-form-urlencoded;charset=UTF-8"], Content = Text.ToBinary(body) ] )),
        datatable = Source[datatable],
        data = datatable[data],
        #"Converted to Table" = Table.FromList(data, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"codistat", "denominazione", "mtot", "ftot", "totmf"}, {"codistat", "denominazione", "mtot", "ftot", "totmf"})
    in
        #"Expanded Column1"
in GetProvinceData
The changes to the previous PQ code are to 'embed' the let url .... in #Expanded Column1" structure inside the let GetProvinceData ..... in GetProvinceData structure, with the (ProvinceCode as text) parameter and concatenating the ProvinceCode parameter in the body string, replacing the 084 value.

5. Next, we will invoke the GetProvinceData custom function on each row in the Provinces table to return the data for all 3 provinces.

Select a cell in the Provinces table.
Data tab -> From Table/Range.
Important: In the PQ Editor, change the data type of the Code column to Text (Replace current). For some reason, PQ doesn't recognise that the Excel column is formatted as Text.
On the Add Column tab, click Invoke Custom Function.
In the Invoke Custom Function dialogue, choose GetProvinceData in the Function query dropdown. In the ProvinceCode dropdown, choose Code. Click OK.
If displayed, click Continue on the yellow privacy warning and tick 'Ignore Privacy Levels Checks for this file....' and click Save.
Click the Expand icon next to the GetProvinceData column. Select all 5 columns (codistat, denominazione, mtot, ftot, totmf) and untick 'Use original column name as prefix'.
On the Home tab, click Close & Load To... Choose Table option, and select the destination cell and worksheet for the new table.

The query returns 277 rows for the 3 provinces:
CodeProvincecodistatdenominazionemtotftottotmf
084Agrigento084001Agrigento271882844855636
084Agrigento084002Alessandria della Rocca120712932500
084Agrigento084003Aragona422045238743
084Agrigento084004Bivona157816483226
084Agrigento084005Burgio121412902504
084Agrigento084006Calamonaci5785951173
084Agrigento084007Caltabellotta152716943221
084Agrigento084008Camastra9389771915
084Agrigento084009Cammarata292029465866
084Agrigento084010Campobello di Licata440946849093
084Agrigento084011Canicattì165731777134344
084Agrigento084012Casteltermini352138257346
084Agrigento084013Castrofilippo121113862597
084Agrigento084014Cattolica Eraclea162016873307
084Agrigento084015Cianciana146016393099
084Agrigento084016Comitini430457887
084Agrigento084017Favara152901631131601
084Agrigento084018Grotte245227715223
084Agrigento084019Joppolo Giancaxio4985631061
084Agrigento084020Lampedusa e Linosa330730536360
084Agrigento084021Licata168401750134341
084Agrigento084022Lucca Sicula8458841729
084Agrigento084023Menfi5674607311747
084Agrigento084024Montallegro116112082369
084Agrigento084025Montevago132813852713
084Agrigento084026Naro337636016977
084Agrigento084027Palma di Montechiaro101341111721251
084Agrigento084028Porto Empedocle7575791115486
084Agrigento084029Racalmuto365038627512
084Agrigento084030Raffadali5840630012140
084Agrigento084031Ravanusa5060539310453
084Agrigento084032Realmonte214622254371
084Agrigento084033Ribera8596927517871
084Agrigento084034Sambuca di Sicilia266727185385
084Agrigento084035San Biagio Platani133915392878
084Agrigento084036San Giovanni Gemini372838047532
084Agrigento084037Santa Elisabetta103211052137
084Agrigento084038Santa Margherita di Belice290830966004
084Agrigento084039Sant'Angelo Muxaro5466101156
084Agrigento084040Santo Stefano Quisquina198721514138
084Agrigento084041Sciacca189742014139115
084Agrigento084042Siculiana200720714078
084Agrigento084043Villafranca Sicula6476951342
006Alessandria006001Acqui Terme88881011419002
006Alessandria006002Albera Ligure170145315
006Alessandria006003Alessandria440634692490987
006Alessandria006004Alfiano Natta364365729
006Alessandria006005Alice Bel Colle384327711
006Alessandria006007Altavilla Monferrato216183399
006Alessandria006008Alzano Scrivia183164347
006Alessandria006009Arquata Scrivia304132156256
006Alessandria006010Avolasca127126253
006Alessandria006011Balzola6236411264
006Alessandria006012Basaluzzo10129932005
006Alessandria006013Bassignana7647821546
006Alessandria006014Belforte Monferrato258240498
006Alessandria006015Bergamasco343366709
006Alessandria006016Berzano di Tortona7681157
006Alessandria006017Bistagno8698871756
006Alessandria006018Borghetto di Borbera9719461917
006Alessandria006019Borgoratto Alessandrino253283536
006Alessandria006020Borgo San Martino6736801353
006Alessandria006021Bosco Marengo115410972251
006Alessandria006022Bosio5465091055
006Alessandria006023Bozzole162143305
006Alessandria006024Brignano-Frascata216206422
006Alessandria006025Cabella Ligure248227475
006Alessandria006026Camagna Monferrato227241468
006Alessandria006027Camino381353734
006Alessandria006028Cantalupo Ligure222232454
006Alessandria006029Capriata d'Orba8888811769
006Alessandria006030Carbonara Scrivia5475511098
006Alessandria006031Carentino165156321
006Alessandria006032Carezzano209215424
006Alessandria006033Carpeneto462449911
006Alessandria006034Carrega Ligure523688
006Alessandria006035Carrosio261233494
006Alessandria006036Cartosio349371720
006Alessandria006037Casal Cermelli5746061180
006Alessandria006038Casaleggio Boiro185185370
006Alessandria006039Casale Monferrato154291709132520
006Alessandria006040Casalnoceto489478967
006Alessandria006041Casasco6259121
006Alessandria006043Cassine142413952819
006Alessandria006044Cassinelle444407851
006Alessandria006045Castellania484088
006Alessandria006046Castellar Guidobono202196398
006Alessandria006047Castellazzo Bormida218622394425
006Alessandria006048Castelletto d'Erro6273135
006Alessandria006049Castelletto d'Orba9029341836
006Alessandria006050Castelletto Merli233217450
006Alessandria006051Castelletto Monferrato7197101429
006Alessandria006052Castelnuovo Bormida296316612
006Alessandria006053Castelnuovo Scrivia237224914863
006Alessandria006054Castelspina196196392
006Alessandria006055Cavatore119141260
006Alessandria006056Cella Monte237230467
006Alessandria006057Cereseto206189395
006Alessandria006058Cerreto Grue149153302
006Alessandria006059Cerrina Monferrato6376671304
006Alessandria006060Coniolo230215445
006Alessandria006061Conzano475477952
006Alessandria006062Costa Vescovato165151316
006Alessandria006063Cremolino500495995
006Alessandria006065Denice8387170
006Alessandria006066Dernice8892180
006Alessandria006067Fabbrica Curone286302588
006Alessandria006068Felizzano104410712115
006Alessandria006069Fraconalto162140302
006Alessandria006070Francavilla Bisio255251506
006Alessandria006071Frascaro214235449
006Alessandria006072Frassinello Monferrato225245470
006Alessandria006073Frassineto Po6636931356
006Alessandria006074Fresonara325298623
006Alessandria006075Frugarolo9019841885
006Alessandria006076Fubine Monferrato7708081578
006Alessandria006077Gabiano5224951017
006Alessandria006078Gamalero382434816
006Alessandria006079Garbagna301313614
006Alessandria006081Gavi218822444432
006Alessandria006082Giarole311359670
006Alessandria006083Gremiasco148143291
006Alessandria006084Grognardo119102221
006Alessandria006085Grondona228248476
006Alessandria006086Guazzora146148294
006Alessandria006087Isola Sant'Antonio331311642
006Alessandria006088Lerma380421801
006Alessandria006090Malvicino354479
006Alessandria006091Masio6446121256
006Alessandria006092Melazzo6166261242
006Alessandria006093Merana9392185
006Alessandria006094Mirabello Monferrato6026051207
006Alessandria006095Molare95110471998
006Alessandria006096Molino dei Torti303261564
006Alessandria006097Mombello Monferrato441480921
006Alessandria006098Momperone10491195
006Alessandria006099Moncestino92103195
006Alessandria006100Mongiardino Ligure8272154
006Alessandria006101Monleale275277552
006Alessandria006102Montacuto132119251
006Alessandria006103Montaldeo108124232
006Alessandria006104Montaldo Bormida309296605
006Alessandria006105Montecastello143147290
006Alessandria006106Montechiaro d'Acqui259241500
006Alessandria006107Montegioco151133284
006Alessandria006108Montemarzino158152310
006Alessandria006109Morano sul Po6376691306
006Alessandria006110Morbello202186388
006Alessandria006111Mornese339369708
006Alessandria006112Morsasco292313605
006Alessandria006113Murisengo6216611282
006Alessandria006114Novi Ligure131801426227442
006Alessandria006115Occimiano5926541246
006Alessandria006116Odalengo Grande207201408
006Alessandria006117Odalengo Piccolo124115239
006Alessandria006118Olivola5460114
006Alessandria006119Orsara Bormida198202400
006Alessandria006120Ottiglio296296592
006Alessandria006121Ovada5187566110848
006Alessandria006122Oviglio5886041192
006Alessandria006123Ozzano Monferrato6816771358
006Alessandria006124Paderna10197198
006Alessandria006125Pareto279237516
006Alessandria006126Parodi Ligure315295610
006Alessandria006127Pasturana6136701283
006Alessandria006128Pecetto di Valenza5765761152
006Alessandria006129Pietra Marazzi417447864
006Alessandria006131Pomaro Monferrato144162306
006Alessandria006132Pontecurone172416883412
006Alessandria006133Pontestura6346681302
006Alessandria006134Ponti279263542
006Alessandria006135Ponzano Monferrato155159314
006Alessandria006136Ponzone5274731000
006Alessandria006137Pozzol Groppo152143295
006Alessandria006138Pozzolo Formigaro226922384507
006Alessandria006139Prasco241237478
006Alessandria006140Predosa9229641886
006Alessandria006141Quargnento6496891338
006Alessandria006142Quattordio7337661499
006Alessandria006143Ricaldone319292611
006Alessandria006144Rivalta Bormida6777001377
006Alessandria006145Rivarone229162391
006Alessandria006146Roccaforte Ligure7449123
006Alessandria006147Rocca Grimalda7406851425
006Alessandria006148Rocchetta Ligure11999218
006Alessandria006149Rosignano Monferrato6987571455
006Alessandria006150Sala Monferrato163177340
006Alessandria006151Sale190120023903
006Alessandria006152San Cristoforo258281539
006Alessandria006153San Giorgio Monferrato5836181201
006Alessandria006154San Salvatore Monferrato200320404043
006Alessandria006155San Sebastiano Curone247280527
006Alessandria006156Sant'Agata Fossili175192367
006Alessandria006157Sardigliano186200386
006Alessandria006158Sarezzano5665611127
006Alessandria006159Serralunga di Crea259249508
006Alessandria006160Serravalle Scrivia292229695891
006Alessandria006161Sezzadio5586021160
006Alessandria006162Silvano d'Orba9629531915
006Alessandria006163Solero7677911558
006Alessandria006164Solonghello10293195
006Alessandria006165Spigno Monferrato462453915
006Alessandria006166Spineto Scrivia180183363
006Alessandria006167Stazzano114211872329
006Alessandria006168Strevi9509511901
006Alessandria006169Tagliolo Monferrato7557321487
006Alessandria006170Tassarolo309290599
006Alessandria006171Terruggia427483910
006Alessandria006172Terzo426411837
006Alessandria006173Ticineto6216781299
006Alessandria006174Tortona127471379826545
006Alessandria006175Treville128134262
006Alessandria006176Trisobbio331336667
006Alessandria006177Valenza8582945818040
006Alessandria006178Valmacca452511963
006Alessandria006179Vignale Monferrato446498944
006Alessandria006180Vignole Borbera98610332019
006Alessandria006181Viguzzolo152415233047
006Alessandria006182Villadeati238241479
006Alessandria006183Villalvernia429445874
006Alessandria006184Villamiroglio153145298
006Alessandria006185Villanova Monferrato8409101750
006Alessandria006186Villaromagnano323331654
006Alessandria006187Visone5815461127
006Alessandria006188Volpedo5665891155
006Alessandria006189Volpeglino6964133
006Alessandria006190Voltaggio326340666
006Alessandria006191Cassano Spinola8879401827
006Alessandria006192Alluvioni Piovera7908171607
006Alessandria006193Lu e Cuccaro Monferrato6436821325
042Ancona042001Agugliano230923874696
042Ancona042002Ancona477475091798664
042Ancona042003Arcevia210021494249
042Ancona042004Barbara6386291267
042Ancona042005Belvedere Ostrense103610652101
042Ancona042006Camerano346536437108
042Ancona042007Camerata Picena125512462501
042Ancona042008Castelbellino236325714934
042Ancona042010Castelfidardo9072938618458
042Ancona042011Castelleone di Suasa7987891587
042Ancona042012Castelplanio172817783506
042Ancona042013Cerreto d'Esi169017183408
042Ancona042014Chiaravalle6720754614266
042Ancona042015Corinaldo232824734801
042Ancona042016Cupramontana217022274397
042Ancona042017Fabriano140421506529107
042Ancona042018Falconara Marittima123991323825637
042Ancona042019Filottrano443145498980
042Ancona042020Genga8218411662
042Ancona042021Jesi188292035439183
042Ancona042022Loreto6223665312876
042Ancona042023Maiolati Spontini292131836104
042Ancona042024Mergo4925171009
042Ancona042025Monsano161217043316
042Ancona042026Montecarotto9009501850
042Ancona042027Montemarciano491449309844
042Ancona042029Monte Roberto148815233011
042Ancona042030Monte San Vito329634146710
042Ancona042031Morro d'Alba8819081789
042Ancona042032Numana188618633749
042Ancona042033Offagna103010092039
042Ancona042034Osimo171201762134741
042Ancona042035Ostra311432736387
042Ancona042036Ostra Vetere155515723127
042Ancona042037Poggio San Marcello325325650
042Ancona042038Polverigi227123134584
042Ancona042040Rosora9179431860
042Ancona042041San Marcello99210262018
042Ancona042042San Paolo di Jesi430456886
042Ancona042043Santa Maria Nuova195320233976
042Ancona042044Sassoferrato333535236858
042Ancona042045Senigallia211182297044088
042Ancona042046Serra de' Conti178318103593
042Ancona042047Serra San Quirico120613532559
042Ancona042048Sirolo196521304095
042Ancona042049Staffolo102010502070
042Ancona042050Trecastelli373837067444


Hopefully, all the steps work for you, but let us know if you get stuck.
 
Upvote 0
Ok, I'll try to guide you through the steps to get the data for multiple provinces. Power Query is relatively new to me so some steps might not be optimum.

1. First, create the following table on a sheet and rename it Provinces.

CodeProvince
084Agrigento
006Alessandria
042Ancona

The Code column is formatted as Text, and I had to type in the leading zeroes to make all the codes 3 digits long. For this guide, the Provinces table contains only 3 provinces but you can easily expand it to all the provinces.

2. Note - you could skip steps 2 and 3 and go directly to step 4 by creating a blank connection-only query named GetProvinceData, but here I will show you all the individual manual steps needed to get to the web data.

Next, create the basic query which sends a POST request for the province with code 084:

Data tab -> Get Data -> Other sources -> Blank Query. In Advanced Editor, replace everything with:

Power Query:
let
    url = "https://demo.istat.it/app/RPCCerca.php",
    body  = "territorio=procom&province=084&hid-i=POS&hid-a=2022&hid-l=it&hid-cat=POS&hid-dati=dati-form-1&hid-tavola=tavola-form-1",
    Source = Json.Document(Web.Contents(url,[Headers = [#"Content-Type"="application/x-www-form-urlencoded;charset=UTF-8"], Content = Text.ToBinary(body) ] ))
in
    Source
Click Done to close Advanced Editor.

3. Now we will drill down to the required data, which is in JSON format.

Click Record next to datatable.
Click List next to data.
Click To Table on the Transform tab. Click OK in the To Table dialogue.
Click the Expand icon at the top of Column1.
Select 5 columns: codistat, denominazione, mtot, ftot, totmf. Untick 'Use original column name as prefix'. Click OK.
Under Properties, change the name of the query from Query1 to GetProvinceData.
Click Advanced Editor to view the PQ code, which should be:

Power Query:
let
    url = "https://demo.istat.it/app/RPCCerca.php",
    body  = "territorio=procom&province=084&hid-i=POS&hid-a=2022&hid-l=it&hid-cat=POS&hid-dati=dati-form-1&hid-tavola=tavola-form-1",
    Source = Json.Document(Web.Contents(url,[Headers = [#"Content-Type"="application/x-www-form-urlencoded;charset=UTF-8"], Content = Text.ToBinary(body) ] )),
    datatable = Source[datatable],
    data = datatable[data],
    #"Converted to Table" = Table.FromList(data, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"codistat", "denominazione", "mtot", "ftot", "totmf"}, {"codistat", "denominazione", "mtot", "ftot", "totmf"})
in
    #"Expanded Column1"

Click Done to close Advanced Editor.
Click Close & Load To...
Important: Click Only Create Connection. This creates a connection-only query named GetProvinceData.

4. Now change the GetProvinceData query to a custom function which accepts a parameter for the province code:

Edit the GetProvinceData query and in Advanced Editor replace everything with:

Power Query:
let GetProvinceData = (ProvinceCode as text) =>
    let
        url = "https://demo.istat.it/app/RPCCerca.php",
        body  = "territorio=procom&province="&ProvinceCode&"&hid-i=POS&hid-a=2022&hid-l=it&hid-cat=POS&hid-dati=dati-form-1&hid-tavola=tavola-form-1",
        Source = Json.Document(Web.Contents(url,[Headers = [#"Content-Type"="application/x-www-form-urlencoded;charset=UTF-8"], Content = Text.ToBinary(body) ] )),
        datatable = Source[datatable],
        data = datatable[data],
        #"Converted to Table" = Table.FromList(data, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"codistat", "denominazione", "mtot", "ftot", "totmf"}, {"codistat", "denominazione", "mtot", "ftot", "totmf"})
    in
        #"Expanded Column1"
in GetProvinceData
The changes to the previous PQ code are to 'embed' the let url .... in #Expanded Column1" structure inside the let GetProvinceData ..... in GetProvinceData structure, with the (ProvinceCode as text) parameter and concatenating the ProvinceCode parameter in the body string, replacing the 084 value.

5. Next, we will invoke the GetProvinceData custom function on each row in the Provinces table to return the data for all 3 provinces.

Select a cell in the Provinces table.
Data tab -> From Table/Range.
Important: In the PQ Editor, change the data type of the Code column to Text (Replace current). For some reason, PQ doesn't recognise that the Excel column is formatted as Text.
On the Add Column tab, click Invoke Custom Function.
In the Invoke Custom Function dialogue, choose GetProvinceData in the Function query dropdown. In the ProvinceCode dropdown, choose Code. Click OK.
If displayed, click Continue on the yellow privacy warning and tick 'Ignore Privacy Levels Checks for this file....' and click Save.
Click the Expand icon next to the GetProvinceData column. Select all 5 columns (codistat, denominazione, mtot, ftot, totmf) and untick 'Use original column name as prefix'.
On the Home tab, click Close & Load To... Choose Table option, and select the destination cell and worksheet for the new table.

The query returns 277 rows for the 3 provinces:
CodeProvincecodistatdenominazionemtotftottotmf
084Agrigento084001Agrigento271882844855636
084Agrigento084002Alessandria della Rocca120712932500
084Agrigento084003Aragona422045238743
084Agrigento084004Bivona157816483226
084Agrigento084005Burgio121412902504
084Agrigento084006Calamonaci5785951173
084Agrigento084007Caltabellotta152716943221
084Agrigento084008Camastra9389771915
084Agrigento084009Cammarata292029465866
084Agrigento084010Campobello di Licata440946849093
084Agrigento084011Canicattì165731777134344
084Agrigento084012Casteltermini352138257346
084Agrigento084013Castrofilippo121113862597
084Agrigento084014Cattolica Eraclea162016873307
084Agrigento084015Cianciana146016393099
084Agrigento084016Comitini430457887
084Agrigento084017Favara152901631131601
084Agrigento084018Grotte245227715223
084Agrigento084019Joppolo Giancaxio4985631061
084Agrigento084020Lampedusa e Linosa330730536360
084Agrigento084021Licata168401750134341
084Agrigento084022Lucca Sicula8458841729
084Agrigento084023Menfi5674607311747
084Agrigento084024Montallegro116112082369
084Agrigento084025Montevago132813852713
084Agrigento084026Naro337636016977
084Agrigento084027Palma di Montechiaro101341111721251
084Agrigento084028Porto Empedocle7575791115486
084Agrigento084029Racalmuto365038627512
084Agrigento084030Raffadali5840630012140
084Agrigento084031Ravanusa5060539310453
084Agrigento084032Realmonte214622254371
084Agrigento084033Ribera8596927517871
084Agrigento084034Sambuca di Sicilia266727185385
084Agrigento084035San Biagio Platani133915392878
084Agrigento084036San Giovanni Gemini372838047532
084Agrigento084037Santa Elisabetta103211052137
084Agrigento084038Santa Margherita di Belice290830966004
084Agrigento084039Sant'Angelo Muxaro5466101156
084Agrigento084040Santo Stefano Quisquina198721514138
084Agrigento084041Sciacca189742014139115
084Agrigento084042Siculiana200720714078
084Agrigento084043Villafranca Sicula6476951342
006Alessandria006001Acqui Terme88881011419002
006Alessandria006002Albera Ligure170145315
006Alessandria006003Alessandria440634692490987
006Alessandria006004Alfiano Natta364365729
006Alessandria006005Alice Bel Colle384327711
006Alessandria006007Altavilla Monferrato216183399
006Alessandria006008Alzano Scrivia183164347
006Alessandria006009Arquata Scrivia304132156256
006Alessandria006010Avolasca127126253
006Alessandria006011Balzola6236411264
006Alessandria006012Basaluzzo10129932005
006Alessandria006013Bassignana7647821546
006Alessandria006014Belforte Monferrato258240498
006Alessandria006015Bergamasco343366709
006Alessandria006016Berzano di Tortona7681157
006Alessandria006017Bistagno8698871756
006Alessandria006018Borghetto di Borbera9719461917
006Alessandria006019Borgoratto Alessandrino253283536
006Alessandria006020Borgo San Martino6736801353
006Alessandria006021Bosco Marengo115410972251
006Alessandria006022Bosio5465091055
006Alessandria006023Bozzole162143305
006Alessandria006024Brignano-Frascata216206422
006Alessandria006025Cabella Ligure248227475
006Alessandria006026Camagna Monferrato227241468
006Alessandria006027Camino381353734
006Alessandria006028Cantalupo Ligure222232454
006Alessandria006029Capriata d'Orba8888811769
006Alessandria006030Carbonara Scrivia5475511098
006Alessandria006031Carentino165156321
006Alessandria006032Carezzano209215424
006Alessandria006033Carpeneto462449911
006Alessandria006034Carrega Ligure523688
006Alessandria006035Carrosio261233494
006Alessandria006036Cartosio349371720
006Alessandria006037Casal Cermelli5746061180
006Alessandria006038Casaleggio Boiro185185370
006Alessandria006039Casale Monferrato154291709132520
006Alessandria006040Casalnoceto489478967
006Alessandria006041Casasco6259121
006Alessandria006043Cassine142413952819
006Alessandria006044Cassinelle444407851
006Alessandria006045Castellania484088
006Alessandria006046Castellar Guidobono202196398
006Alessandria006047Castellazzo Bormida218622394425
006Alessandria006048Castelletto d'Erro6273135
006Alessandria006049Castelletto d'Orba9029341836
006Alessandria006050Castelletto Merli233217450
006Alessandria006051Castelletto Monferrato7197101429
006Alessandria006052Castelnuovo Bormida296316612
006Alessandria006053Castelnuovo Scrivia237224914863
006Alessandria006054Castelspina196196392
006Alessandria006055Cavatore119141260
006Alessandria006056Cella Monte237230467
006Alessandria006057Cereseto206189395
006Alessandria006058Cerreto Grue149153302
006Alessandria006059Cerrina Monferrato6376671304
006Alessandria006060Coniolo230215445
006Alessandria006061Conzano475477952
006Alessandria006062Costa Vescovato165151316
006Alessandria006063Cremolino500495995
006Alessandria006065Denice8387170
006Alessandria006066Dernice8892180
006Alessandria006067Fabbrica Curone286302588
006Alessandria006068Felizzano104410712115
006Alessandria006069Fraconalto162140302
006Alessandria006070Francavilla Bisio255251506
006Alessandria006071Frascaro214235449
006Alessandria006072Frassinello Monferrato225245470
006Alessandria006073Frassineto Po6636931356
006Alessandria006074Fresonara325298623
006Alessandria006075Frugarolo9019841885
006Alessandria006076Fubine Monferrato7708081578
006Alessandria006077Gabiano5224951017
006Alessandria006078Gamalero382434816
006Alessandria006079Garbagna301313614
006Alessandria006081Gavi218822444432
006Alessandria006082Giarole311359670
006Alessandria006083Gremiasco148143291
006Alessandria006084Grognardo119102221
006Alessandria006085Grondona228248476
006Alessandria006086Guazzora146148294
006Alessandria006087Isola Sant'Antonio331311642
006Alessandria006088Lerma380421801
006Alessandria006090Malvicino354479
006Alessandria006091Masio6446121256
006Alessandria006092Melazzo6166261242
006Alessandria006093Merana9392185
006Alessandria006094Mirabello Monferrato6026051207
006Alessandria006095Molare95110471998
006Alessandria006096Molino dei Torti303261564
006Alessandria006097Mombello Monferrato441480921
006Alessandria006098Momperone10491195
006Alessandria006099Moncestino92103195
006Alessandria006100Mongiardino Ligure8272154
006Alessandria006101Monleale275277552
006Alessandria006102Montacuto132119251
006Alessandria006103Montaldeo108124232
006Alessandria006104Montaldo Bormida309296605
006Alessandria006105Montecastello143147290
006Alessandria006106Montechiaro d'Acqui259241500
006Alessandria006107Montegioco151133284
006Alessandria006108Montemarzino158152310
006Alessandria006109Morano sul Po6376691306
006Alessandria006110Morbello202186388
006Alessandria006111Mornese339369708
006Alessandria006112Morsasco292313605
006Alessandria006113Murisengo6216611282
006Alessandria006114Novi Ligure131801426227442
006Alessandria006115Occimiano5926541246
006Alessandria006116Odalengo Grande207201408
006Alessandria006117Odalengo Piccolo124115239
006Alessandria006118Olivola5460114
006Alessandria006119Orsara Bormida198202400
006Alessandria006120Ottiglio296296592
006Alessandria006121Ovada5187566110848
006Alessandria006122Oviglio5886041192
006Alessandria006123Ozzano Monferrato6816771358
006Alessandria006124Paderna10197198
006Alessandria006125Pareto279237516
006Alessandria006126Parodi Ligure315295610
006Alessandria006127Pasturana6136701283
006Alessandria006128Pecetto di Valenza5765761152
006Alessandria006129Pietra Marazzi417447864
006Alessandria006131Pomaro Monferrato144162306
006Alessandria006132Pontecurone172416883412
006Alessandria006133Pontestura6346681302
006Alessandria006134Ponti279263542
006Alessandria006135Ponzano Monferrato155159314
006Alessandria006136Ponzone5274731000
006Alessandria006137Pozzol Groppo152143295
006Alessandria006138Pozzolo Formigaro226922384507
006Alessandria006139Prasco241237478
006Alessandria006140Predosa9229641886
006Alessandria006141Quargnento6496891338
006Alessandria006142Quattordio7337661499
006Alessandria006143Ricaldone319292611
006Alessandria006144Rivalta Bormida6777001377
006Alessandria006145Rivarone229162391
006Alessandria006146Roccaforte Ligure7449123
006Alessandria006147Rocca Grimalda7406851425
006Alessandria006148Rocchetta Ligure11999218
006Alessandria006149Rosignano Monferrato6987571455
006Alessandria006150Sala Monferrato163177340
006Alessandria006151Sale190120023903
006Alessandria006152San Cristoforo258281539
006Alessandria006153San Giorgio Monferrato5836181201
006Alessandria006154San Salvatore Monferrato200320404043
006Alessandria006155San Sebastiano Curone247280527
006Alessandria006156Sant'Agata Fossili175192367
006Alessandria006157Sardigliano186200386
006Alessandria006158Sarezzano5665611127
006Alessandria006159Serralunga di Crea259249508
006Alessandria006160Serravalle Scrivia292229695891
006Alessandria006161Sezzadio5586021160
006Alessandria006162Silvano d'Orba9629531915
006Alessandria006163Solero7677911558
006Alessandria006164Solonghello10293195
006Alessandria006165Spigno Monferrato462453915
006Alessandria006166Spineto Scrivia180183363
006Alessandria006167Stazzano114211872329
006Alessandria006168Strevi9509511901
006Alessandria006169Tagliolo Monferrato7557321487
006Alessandria006170Tassarolo309290599
006Alessandria006171Terruggia427483910
006Alessandria006172Terzo426411837
006Alessandria006173Ticineto6216781299
006Alessandria006174Tortona127471379826545
006Alessandria006175Treville128134262
006Alessandria006176Trisobbio331336667
006Alessandria006177Valenza8582945818040
006Alessandria006178Valmacca452511963
006Alessandria006179Vignale Monferrato446498944
006Alessandria006180Vignole Borbera98610332019
006Alessandria006181Viguzzolo152415233047
006Alessandria006182Villadeati238241479
006Alessandria006183Villalvernia429445874
006Alessandria006184Villamiroglio153145298
006Alessandria006185Villanova Monferrato8409101750
006Alessandria006186Villaromagnano323331654
006Alessandria006187Visone5815461127
006Alessandria006188Volpedo5665891155
006Alessandria006189Volpeglino6964133
006Alessandria006190Voltaggio326340666
006Alessandria006191Cassano Spinola8879401827
006Alessandria006192Alluvioni Piovera7908171607
006Alessandria006193Lu e Cuccaro Monferrato6436821325
042Ancona042001Agugliano230923874696
042Ancona042002Ancona477475091798664
042Ancona042003Arcevia210021494249
042Ancona042004Barbara6386291267
042Ancona042005Belvedere Ostrense103610652101
042Ancona042006Camerano346536437108
042Ancona042007Camerata Picena125512462501
042Ancona042008Castelbellino236325714934
042Ancona042010Castelfidardo9072938618458
042Ancona042011Castelleone di Suasa7987891587
042Ancona042012Castelplanio172817783506
042Ancona042013Cerreto d'Esi169017183408
042Ancona042014Chiaravalle6720754614266
042Ancona042015Corinaldo232824734801
042Ancona042016Cupramontana217022274397
042Ancona042017Fabriano140421506529107
042Ancona042018Falconara Marittima123991323825637
042Ancona042019Filottrano443145498980
042Ancona042020Genga8218411662
042Ancona042021Jesi188292035439183
042Ancona042022Loreto6223665312876
042Ancona042023Maiolati Spontini292131836104
042Ancona042024Mergo4925171009
042Ancona042025Monsano161217043316
042Ancona042026Montecarotto9009501850
042Ancona042027Montemarciano491449309844
042Ancona042029Monte Roberto148815233011
042Ancona042030Monte San Vito329634146710
042Ancona042031Morro d'Alba8819081789
042Ancona042032Numana188618633749
042Ancona042033Offagna103010092039
042Ancona042034Osimo171201762134741
042Ancona042035Ostra311432736387
042Ancona042036Ostra Vetere155515723127
042Ancona042037Poggio San Marcello325325650
042Ancona042038Polverigi227123134584
042Ancona042040Rosora9179431860
042Ancona042041San Marcello99210262018
042Ancona042042San Paolo di Jesi430456886
042Ancona042043Santa Maria Nuova195320233976
042Ancona042044Sassoferrato333535236858
042Ancona042045Senigallia211182297044088
042Ancona042046Serra de' Conti178318103593
042Ancona042047Serra San Quirico120613532559
042Ancona042048Sirolo196521304095
042Ancona042049Staffolo102010502070
042Ancona042050Trecastelli373837067444


Hopefully, all the steps work for you, but let us know if you get stuck.

SUPER GREAT!
 
Upvote 0

Forum statistics

Threads
1,215,695
Messages
6,126,262
Members
449,307
Latest member
Andile

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top