Association loi de 1901/SQL

Aller à la navigation Aller à la recherche

Requête web pour l'extraction de données.

Date d'événement

<#
Nom           : PSHWEB101
Description   : extraction de données d'un site web contenant les dates et lieux d'événements
Usage         : exercice en PowerShell
Particularité : néant
Auteur        : fylip22
Version       : 1.0
Révisions     :
                - 1.0 (21/04/2025) : création du script
#>

# nombre maximum d'orchestre à traiter
$ListeLongueur = 200

$OrchestreNombre, $DateNombre = 0, 0
$horodatage = Get-Date -Format "yyyyMMdd-HHmmss"
$fichierExport = "C:\support\bal-$horodatage.csv"

New-Item -ItemType File -Path $fichierExport -Force 
Set-Content -Path $fichierExport -Value "Orchestre;Jour;Date;Heure;Ville;Département;Salle;Descriptif" -Encoding UTF8

# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
# 
# A.- Récupération de la liste des orchestres (code)
# 
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

# page de la liste des orchestres
$webSite = Invoke-WebRequest -Uri http://www.touteladanse.com/dd_orchestre.php

# récupération des éléments du site
$liste = $webSite.AllElements
$match = $liste | select-string '([a-z]{3}[0-9]{3})' -AllMatches
$resultat = $match.Matches.value

$orchestreListeUnique = $resultat | Select-Object -Unique | Select-Object -last $ListeLongueur | Sort-Object
Write-host ("Nombre d'orchestres : {0}" -f $orchestreListeUnique.count)

Clear-Host 

# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
# 
# B.- Récupération des dates de chaque orchestre
# 
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

## Début de la bouche générale
Foreach ($orchestreCode in $orchestreListeUnique) {
    
   # exploration 
   $webSite = Invoke-WebRequest -Uri http://www.touteladanse.com/$orchestreCode.php

   # Find table in the website
   $tableData = $webSite.AllElements | Where-Object {$_.tagname -eq 'td' -and $_.innertext -notlike "*dispo*"}

   # Table header and data
   $tdata = $tableData.innerText
   $thead = @("Date", "Heure", "Ville", "Salle", "Descriptif")
   $dataResult = New-Object System.Collections.ArrayList

   # commencer à 1 pour ne pas avoir les deux premières (titre du tableau)
   for ($i = 1; $i -le $tdata.count; $i+= ($thead.count - 1)) {
      if ($tdata.count -eq $i) { break }
      $group = $i + ($thead.count - 1)
      [void]$dataResult.Add($tdata[$i..$group])
      $i++
      }

   # Html data into powershell table format
   $finalResult = @()
   $finalResult += $orchestreCode
   $OrchestreNombre += 1

   foreach ($data in $dataResult) {
      $data = $data -replace '(lundi|mardi|mercredi|jeudi|vendredi|samedi|dimanche).(\d\d\/\d\d\/\d\d\d\d)$', '$1;$2'
      $regex = "([abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZâÉéèêëîïôö'' -]+).*\(([0-9][0-9])\)"
      $data = $data -replace $regex,'$1;$2'
      $data = $data -join ";"
      $finalResult += $data -join ";"
      Add-Content -Path $fichierExport -Value "$orchestreCode;$data"
   }

# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
# 
# B.1 - Affichage des dates pour l'orchestre en cours
# 
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

   Write-Host "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
   Write-Host ("Orchestre {0}, nombre de dates : {1}" -f $orchestreCode, $finalResult.count)
   $DateNombre += $finalResult.count
}
## Fin de la bouche générale

Write-Host ("Fin du traitement pour {0} orchestres et {1} événements" -f $OrchestreNombre, $DateNombre)

Voir aussi

  •