2

サイトを解析する小さなパーサーがあります - 6150 のレコードがあります。しかし、これを CSV 形式にする必要があります。

まず、対象サイトをご覧ください: http://192.68.214.70/km/asps/schulsuche.asp?q=a&a=50&s=1750

すべてのデータが必要です - のフィールドに分離されています

number
schoolnumber
school-name
Adress
Street
Postal Code
phone
fax
School-type
website

ええと、私には台本があります。これについてあなたがどう思うか非常に興味があります。まだすべてのフィールドを獲得しているわけではありません - もっと多くのフィールドが必要です!

#!/usr/bin/perl
use strict;
use HTML::TableExtract;
use LWP::Simple;
use Cwd;
use POSIX qw(strftime);

my $total_records = 0;
my $alpha = "x";
my $results = 50;
my $range = 0;
my $url_to_process = "http://192.68.214.70/km/asps/schulsuche.asp?q=";
my $processdir = "processing";
my $counter = 50;
my $percent = 0;

workDir();
chdir $processdir;
processURL();
print "\nPress <enter> to continue\n";
<>;
my $displaydate = strftime('%Y%m%d%H%M%S', localtime);
open my $outfile, '>', "webdata_for_$alpha\_$displaydate.txt" or die 'Unable to create file';
processData();
close $outfile;
print "Finished processing $total_records records...\n";
print "Processed data saved to $ENV{HOME}/$processdir/webdata_for_$alpha\_$displaydate.txt\n";
unlink 'processing.html';

sub processURL() {
print "\nProcessing $url_to_process$alpha&a=$results&s=$range\n";
getstore("$url_to_process$alpha&a=$results&s=$range", 'tempfile.html') or die 'Unable to get page';

   while( <tempfile.html> ) {
      open( FH, "$_" ) or die;
      while( <FH> ) {
         if( $_ =~ /^.*?(Treffer \<b\>)(\d+)( - )(\d+)(<\/b> \w+ \w+ \<b\>)(\d+).*/ ) {
            $total_records = $6;
            print "Total records to process is $total_records\n";
            }
         }
         close FH;
   }
   unlink 'tempfile.html';
}

sub processData() {
   while ( $range <= $total_records) {
      my $te = HTML::TableExtract->new(headers => [qw(lfd Schul Schulname Telefon Schulart Webseite)]);
      getstore("$url_to_process$alpha&a=$results&s=$range", 'processing.html') or die 'Unable to get page';
      $te->parse_file('processing.html');
      my ($table) = $te->tables;
      foreach my $ts ($te->table_states) {
         foreach my $row ($ts->rows) {
            cleanup(@$row);
        # Add a table column delimiter in this case ||
            print $outfile join("||", @$row)."\n";
            }
         }
      $| = 1; 
      print "Processed records $range to $counter";
      print "\r";
      $counter = $counter + 50;
      $range = $range + 50;
   }
}

sub cleanup() {
   for ( @_ ) {
      s/\s+/ /g;
   }
}

sub workDir() {
# Use home directory to process data
chdir or die "$!";
if ( ! -d $processdir ) {
   mkdir ("$ENV{HOME}/$processdir", 0755) or die "Cannot make directory $processdir: $!";
   }
}

次の出力で:

1||9752||Deutsche Schule Alamogordo  USA  Alamogorde - New Mexico  || ||Deutschsprachige Auslandsschule||
2||9931||Deutsche Schule der Borromäerinnen Alexandrien ET  Alexandrien - Ägypten  || ||Begegnungsschule (Auslandsschuldienst)||
3||1940||Max-Keller-Schule, Berufsfachschule f.Musik Alt- ötting d.Berufsfachschule für Musik Altötting e.V. Kapellplatz 36 84503  Altötting  ||08671/1735 08671/84363||Berufsfachschulen f. Musik|| www.max-keller-schule.de
4||0006||Max-Reger-Gymnasium Amberg  Kaiser-Wilhelm-Ring 7 92224  Amberg  ||09621/4718-0 09621/4718-47||Gymnasien|| www.mrg-amberg.de

|| で 区切り文字です。

私の問題は、より多くのフィールドが必要なことです-次のように分割する必要があります-例を参照してください:

name: Volksschule Abenberg (Grundschule)
street: Güssübelstr. 2
postal-code and town: 91183 Abenberg
fax and telephone: 09178/215 09178/905060
type of school: Volksschulen
website: home.t-online.de/home/vs-abenberg

フィールドをさらに追加するには?これは明らかにこの行で実行する必要がありますね!?

 my $te = HTML::TableExtract->new(headers => [qw(lfd Schul Schulname Telefon Schulart Webseite)]);

しかし、どのように?いろいろ試しましたが、いつも悪い結果しか出ませんでした。私は遊んで、別の解決策を試しましたが、ここには良いCSVデータがありますが、残念ながらスパイダーロジックはありません...

#!/usr/bin/perl
use warnings;
use strict;
use LWP::Simple;
use HTML::TableExtract;
use Text::CSV;

my $html= get 'http://192.68.214.70/km/asps/schulsuche.asp?q=n&a=50';
$html =~ tr/r//d;     # strip the carriage returns
$html =~ s/&nbsp;/ /g; # expand the spaces

my $te = new HTML::TableExtract();
$te->parse($html);

my @cols = qw(
    rownum
    number
    name
    phone
    type
    website
);

my @fields = qw(
    rownum
    number
    name
    street
    postal
    town
    phone
    fax
    type
    website
);

my $csv = Text::CSV->new({ binary => 1 });

foreach my $ts ($te->table_states) {
    foreach my $row ($ts->rows) {

        #  trim leading/trailing whitespace from base fields
        s/^s+//, s/\s+$// for @$row;

        # load the fields into the hash using a "hash slice"
        my %h;
        @h{@cols} = @$row;

        # derive some fields from base fields, again using a hash slice
        @h{qw/name street postal town/} = split /n+/, $h{name};
        @h{qw/phone fax/} = split /n+/, $h{phone};

        #  trim leading/trailing whitespace from derived fields
        s/^s+//, s/\s+$// for @h{qw/name street postal town/};

        $csv->combine(@h{@fields});
        print $csv->string, "\n";
    }
} 

まあ-これで別の解決策を試しました-しかし、ここには良いCSVデータがあります-残念ながらスパイダーロジックはありません。

ここにスパイダーロジックを追加する方法!?

最初のスクリプトまたは 2 番目のスクリプトで、助けが必要です。

4

1 に答える 1

0

Webサイトではbr、データを分割するのと同じように、タグを使用して各セル内のサブフィールドを区切ります。HTML::TableExtract最初のプログラムでは、これらはデフォルトで改行に変換されますが、cleanupルーチンはこの情報を破棄します。

s/\n/||/sg;最初のプログラムでは、残りの空白を平坦化する前に、(同じセパレーターを想定して)のようなものを追加します。

于 2011-05-29T23:41:06.527 に答える