서울 구글 맵 Phone Extract – ORIGINAL

#!/usr/bin/perl

use strict;
use warnings;
use HTML::TokeParser;

# Input file containing a list of file names
my $file_list = 'moving_seoul.txt';

# Output file for saving extracted phone numbers
my $output_file = 'moving_seoul_extracted.txt';

# Open the file list for reading
open my $file_list_fh, '<', $file_list or die "Cannot open file list '$file_list': $!";

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

# Iterate through each file listed in the input file
while (<$file_list_fh>) {
    chomp;  # Remove newline characters
    my $input_file = $_;

    # 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 print the text content
            my $content = $token->[1];
            print $output_fh "$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 file list and output file
close $file_list_fh;
close $output_fh;

# Print a completion message
print "Extraction complete. Extracted content between <span class=\"UsdlK\">, <div class=\"W4Efsd\">, and their corresponding closing tags is saved in '$output_file'\n";

Leave a Reply

Your email address will not be published. Required fields are marked *