bizname.pl

each downloaded html bizname extraction perl:

#!/usr/bin/perl
use strict;
use warnings;
use HTML::TokeParser;

# Define the filename containing the list of HTML files
my $filename = "file_names.txt";

# Open the file containing the list of HTML files
open my $file_list_fh, '<', $filename or die "Cannot open file $filename: $!";

# Open the output file for writing
open my $output_fh, '>', "bizname.txt" or die "Cannot open file bizname.txt for writing: $!";

# Loop through each line of the file
while (my $html_file = <$file_list_fh>) {
    chomp $html_file; # Remove newline character
    next if $html_file =~ /^\s*$/; # Skip empty lines

    # Remove leading and trailing whitespace from the filename
    $html_file =~ s/^\s+|\s+$//g;

    # Open and read the HTML file
    open my $html_fh, '<', $html_file or die "Cannot open file $html_file: $/";
    my $html_content = do { local $/; <$html_fh> };
    close $html_fh;

    # Create a new HTML::TokeParser object
    my $parser = HTML::TokeParser->new(\$html_content);

    # Flag to indicate whether we are inside the specified div tag
    my $inside_div = 0;

    my $extracted_content = ""; # Initialize variable to store extracted content

    # Loop through the tokens
    while (my $token = $parser->get_token) {
        if ($token->[0] eq 'S' && $token->[1] eq 'div' && defined $token->[2]{'style'} && $token->[2]{'style'} =~ /margin-top:5px;text-align: left;font-weight:700;/) {
            # Start of the specified div tag
            $inside_div = 1;
        } elsif ($inside_div) {
            if ($token->[0] eq 'T') {
                # Text token inside the div
                $extracted_content .= $token->[1]; # Append content to extracted content
            } elsif ($token->[0] eq 'E' && $token->[1] eq 'br') {
                # End of the div when encountering a br tag
                $inside_div = 0;
                last; # Exit the loop
            }
        }
    }

    # Print the extracted content followed by the filename, separated by a semicolon
    print $output_fh "$extracted_content; $html_file\n";

    print "Extracted content from $html_file\n";
}

# Close the files
close $file_list_fh;
close $output_fh;

print "All extracted content saved to bizname.txt\n";

extract.pl

mechanize downloaded file, extract perl:

#!/usr/bin/perl
use strict;
use warnings;
use HTML::TokeParser;

# Define the filename containing the list of HTML files
my $filename = "file_names.txt";
my $output_file = "extract.txt";

# Open the file containing the list of HTML files
open my $file_list_fh, '<', $filename or die "Cannot open file $filename: $!";
# Open the output file for writing
open my $output_fh, '>', $output_file or die "Cannot open file $output_file: $!";

# Loop through each line of the file
while (my $html_file = <$file_list_fh>) {
    chomp $html_file; # Remove newline character
    next if $html_file =~ /^\s*$/; # Skip empty lines

    # Read the entire HTML file into a string
    open my $html_fh, '<', $html_file or die "Cannot open HTML file $html_file: $!";
    my $html_content = do { local $/; <$html_fh> };
    close $html_fh;

    # Replace <br> tags with semicolons
    $html_content =~ s/<br>/;/g;

    # Now, process the modified HTML content using HTML::TokeParser and extract table values
    my $parser = HTML::TokeParser->new(\$html_content) or die "Cannot create HTML::TokeParser object: $!";
    my ($inside_td) = 0;
    my ($extracted_content, $old_content) = ("", "");

    while (my $token = $parser->get_token) {
        if ($token->[0] eq 'S' && $token->[1] eq 'td' && defined $token->[2]{valign} && $token->[2]{valign} eq 'top') {
            # Start of a table cell
            $inside_td = 1;
        } elsif ($token->[0] eq 'E' && $token->[1] eq 'td') {
            # End of a table cell
            $inside_td = 0;
            if ($extracted_content || $old_content) {
                print $output_fh "$html_file;$extracted_content;$old_content\n"; # Write the filename, extracted content, and old content to the output file if any exists
                ($extracted_content, $old_content) = ("", ""); # Reset the extracted content and old content for the next cell
            }
        } elsif ($token->[0] eq 'T' && $inside_td) {
            # Text token inside a table cell
            $old_content .= $token->[1]; # Accumulate old content if inside <td>
        }
    }
}

# Close the files
close $file_list_fh;
close $output_fh;

extract_TEL.pl (removes leading space of the line having TEL:)

#!/usr/bin/perl
use strict;
use warnings;

# Define the input and output filenames
my $input_filename = 'extract.txt';
my $output_filename = 'extract_TEL.txt';

# Open the input file for reading
open(my $fh_in, '<', $input_filename) or die "Could not open file '$input_filename' $!";
# Open the output file for writing
open(my $fh_out, '>', $output_filename) or die "Could not open file '$output_filename' for writing: $!";

# Loop through each line of the input file
while (my $line = <$fh_in>) {
    chomp $line;  # Remove newline character

    # Remove leading spaces from the line if it contains "TEL:"
    $line =~ s/^\s+// if $line =~ /^\s*TEL:/;

    print $fh_out "$line\n";  # Print the modified line to the output file
}

# Close the file handles
close $fh_in;
close $fh_out;

print "Processing complete. Results saved in $output_filename\n";

extract_TEL_single_line.pl (move TEL: line to above line)

#!/usr/bin/perl
use strict;
use warnings;

# Define the input and output filenames
my $input_filename = 'extract_TEL.txt';
my $output_filename = 'extract_TEL_single_line.txt';

# Open the input file for reading
open(my $fh_in, '<', $input_filename) or die "Could not open file '$input_filename' $!";
# Open the output file for writing
open(my $fh_out, '>', $output_filename) or die "Could not open file '$output_filename' for writing: $!";

my $prev_line = '';  # Initialize a variable to store the previous line

# Loop through each line of the input file
while (my $line = <$fh_in>) {
    chomp $line;  # Remove newline character

    if ($line =~ /^TEL:/) {
        # If the line starts with "TEL:", concatenate it with the previous line
        $prev_line =~ s/\s+$//;  # Remove trailing whitespace from the previous line
        $line =~ s/^\s+//;  # Remove leading whitespace from the current line
        print $fh_out "$prev_line $line\n";
    } else {
        # If the line does not start with "TEL:", print it as is
        print $fh_out "$line\n";
        $prev_line = $line;  # Update the previous line
    }
}

# Close the file handles
close $fh_in;
close $fh_out;

print "Processing complete. Results saved in $output_filename\n";

extract_TEL_single_line_only.pl (only lines having TEL: on it)

#!/usr/bin/perl
use strict;
use warnings;

# Define the input and output filenames
my $input_filename = 'extract_TEL_single_line.txt';
my $output_filename = 'extract_TEL_single_line_only.txt';

# Open the input file for reading
open(my $fh_in, '<', $input_filename) or die "Could not open file '$input_filename' $!";
# Open the output file for writing
open(my $fh_out, '>', $output_filename) or die "Could not open file '$output_filename' for writing: $!";

# Loop through each line of the input file
while (my $line = <$fh_in>) {
    chomp $line;  # Remove newline character

    if ($line =~ /TEL:/) {
        # If the line contains "TEL:", print it to the output file
        print $fh_out "$line\n";
    }
}

# Close the file handles
close $fh_in;
close $fh_out;

print "Processing complete. Lines containing 'TEL:' extracted to $output_filename\n";

extract_TEL_single_line_distinct.pl (distinct lines only)

#!/usr/bin/perl
use strict;
use warnings;

# Define the input and output filenames
my $input_filename = 'extract_TEL_single_line_only.txt';
my $output_filename = 'extract_TEL_single_line_distinct.txt';

# Open the input file for reading
open(my $fh_in, '<', $input_filename) or die "Could not open file '$input_filename' $!";
# Open the output file for writing
open(my $fh_out, '>', $output_filename) or die "Could not open file '$output_filename' for writing: $!";

# Define a hash to store unique lines
my %unique_lines;

# Loop through each line of the input file
while (my $line = <$fh_in>) {
    chomp $line;  # Remove newline character

    if ($line =~ /TEL:/) {
        # If the line contains "TEL:", add it to the hash
        $unique_lines{$line} = 1;
    }
}

# Write the unique lines to the output file
foreach my $line (keys %unique_lines) {
    print $fh_out "$line\n";
}

# Close the file handles
close $fh_in;
close $fh_out;

print "Processing complete. Distinct lines containing 'TEL:' extracted to $output_filename\n";

extract_TEL_single_line_distinct_semicolon.pl (replace | with 😉

#!/usr/bin/perl
use strict;
use warnings;

# Define the input and output filenames
my $input_filename = 'extract_TEL_single_line_distinct.txt';
my $output_filename = 'extract_TEL_single_line_distinct_semicolon.txt';

# Open the input file for reading
open(my $fh_in, '<', $input_filename) or die "Could not open file '$input_filename' $!";
# Open the output file for writing
open(my $fh_out, '>', $output_filename) or die "Could not open file '$output_filename' for writing: $!";

# Loop through each line of the input file
while (my $line = <$fh_in>) {
    chomp $line;  # Remove newline character

    # Replace '|' with ';'
    $line =~ s/\|/;/g;

    # Write the modified line to the output file
    print $fh_out "$line\n";
}

# Close the file handles
close $fh_in;
close $fh_out;

print "Processing complete. '|' replaced with ';' in $output_filename\n";

filename.pl

filename generation to use for extract.pl

#!/usr/bin/perl
use strict;
use warnings;

# Open the output file for writing
open my $output_fh, '>', "file_names.txt" or die "Cannot open file file_names.txt for writing: $!";

# Generate file names from 1.html to 481.html and write them to the output file
for my $i (1..1320) {
    my $file_name = "$i.html";
    print $output_fh "$file_name\n";
}

# Close the file
close $output_fh;

print "File names generated and saved to file_names.txt\n";