use strict; use warnings; use LWP::Simple; use HTML::TreeBuilder; use Text::CSV; # URL of the website my $url = 'https://desmace.com/provincia/asturias/'; # Filter criteria my $fecha_tramite_min = '24/11/2024'; my $fecha_tramite_max = '27/11/2024'; my $fecha_matricula_min = '01/01/1900'; my $fecha_matricula_max = '31/12/2000'; my $prov_matriculacion_filtro = 'ASTURIAS'; # HTML page content my $html = get($url) or die "No se pudo acceder a la URL: $!"; # Parse HTML my $tree = HTML::TreeBuilder->new; $tree->parse($html); # Open csv file my $csv = Text::CSV->new({ binary => 1, eol => "\n" }); open my $fh, ">", "resultados.csv" or die "No se pudo crear el archivo CSV: $!"; # Headings for csv file $csv->print($fh, ['FECHA DEL TRÁMITE', 'TRÁMITE', 'FECHA MATRÍCULA', 'MARCA', 'MODELO', 'BASTIDOR (VIN)', 'PROV. MATRICULACIÓN']); # This is the table that contains data my @rows = $tree->look_down(_tag => 'tr'); foreach my $row (@rows) { my @columns = $row->look_down(_tag => 'td'); my @data; # Extract values from columns foreach my $col (@columns) { push @data, $col->as_text; } # Row filtering if (@data >= 9) { my ($fecha_tramite, $fecha_matricula, $prov_matriculacion) = @data[0, 2, 7]; if ($fecha_tramite ge $fecha_tramite_min && $fecha_tramite le $fecha_tramite_max && $fecha_matricula ge $fecha_matricula_min && $fecha_matricula le $fecha_matricula_max && $prov_matriculacion eq $prov_matriculacion_filtro) { $csv->print($fh, \@data); } } } # Close file close $fh; $tree->delete; print "Datos filtrados guardados en 'resultados.csv'.\n";