#!/usr/bin/perl
use strict;
use warnings;
use HTML::TokeParser;
# Output file for saving extracted content
my $output_file = 'moving_seoul_extracted.txt';
# Open the output file for writing
open my $output_fh, '>', $output_file or die "Cannot open output file '$output_file': $!";
# Iterate through each file from 1.mhtml to 25.mhtml
for my $file_number (1..25) {
my $input_file = "${file_number}.mhtml";
# Open the current input file for reading
open my $input_fh, '<', $input_file or warn "Cannot open input file '$input_file': $!";
# Create an HTML::TokeParser object for the current input file
my $parser = HTML::TokeParser->new($input_fh);
my $extract = 0; # Flag to indicate whether to extract content between span and div tags
# Iterate through the tokens in the current input file
while (my $token = $parser->get_token) {
if ($token->[0] eq 'S') {
my $tag = $token->[1];
# Check if the tag is "span" or "div" with class attribute containing "UsdlK" or "W4Efsd"
my $class = $token->[2]->{class} || '';
if (($tag eq 'span' && $class =~ /UsdlK/) || ($tag eq 'div' && $class =~ /W4Efsd/)) {
$extract = 1; # Set the extract flag to start extracting
next; # Skip the current token
}
}
if ($extract && $token->[0] eq 'T') {
# Extract and write the text content along with the file name to the output file
my $content = $token->[1];
print $output_fh "$input_file: $content\n";
}
if ($extract && $token->[0] eq 'E') {
my $tag = $token->[1];
# Reset the extract flag when the closing span or div tag is encountered
if ($tag eq 'span' || $tag eq 'div') {
$extract = 0;
}
}
}
# Close the current input file
close $input_fh;
}
# Close the output file
close $output_fh;
# Print a completion message
print "Extraction complete. Extracted content with file names is saved in '$output_file'\n";