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";