1

別の骨の折れるタイトル...申し訳ありません...とにかく、次mash.txtのようなURLの束で呼び出されたファイルがあります:

http://www...

http://www...

http://www...

.

.

.

したがって、この時点で、これらの (URL) を配列にフィードしたいと思います (おそらく途中で何も宣言する必要はありません)。その後、それぞれから再帰的に HTML データを吸い上げ、すべてを同じ URL に追加します。ファイル--作成する必要があると思います...とにかく、事前に感謝します。


value実際には、完全に近いうちに、設計上、各 HTML タグのタグの下の値 ( ) をこのドキュメントに一致させたいoptionので、そのようなゴミはすべてありません...つまり、これらのそれぞれ

http://www...

このようなものを生成します

<!DOCTYPE html>
<HTML>
   <HEAD>
      <TITLE>
         DATA! 
      </TITLE>
   </HEAD>
<BODY>
.
.
.

これらすべての中で必要なのは、この の各 HTML で発生する タグvalueの下の名前だけです。optionmash.txt

4

1 に答える 1

1

以下は、mash.txt 内の各 URL の HTML コンテンツを取得し、すべてのオプションのすべての値を取得して、それらを 1 つの配列にプッシュします。次に、結果の配列が input.template に渡され、処理された出力が output.html に書き込まれます。

#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use HTML::TreeBuilder;
use Template;

my %values;
my $input_file     = 'mash.txt';
my $input_template = 'input.template';
my $output_file    = 'output.html';

# create a new lwp user agent object (our browser).
my $ua = LWP::UserAgent->new( );

# open the input file (mash.txt) for reading.
open my $fh, '<', $input_file or die "cannot open '$input_file': $!";

# iterate through each line (url) in the input file.
while ( my $url = <$fh> )
{
    # get the html contents from url. It returns a handy response object.
    my $response = $ua->get( $url );

    # if we successfully got the html contents from url.
    if ( $response->is_success ) 
    {
        # create a new html tree builder object (our html parser) from the html content.
        my $tb = HTML::TreeBuilder->new_from_content( $response->decoded_content );

        # fetch values across options and push them into the values array.
        # look_down returns an array of option node objects, which we translate to the value of the value attribute via attr upon map.
        $values{$_} = undef for ( map { $_->attr( 'value' ) } $tb->look_down( _tag => 'option' ) );
    }
    # else we failed to get the html contents from url.
    else 
    {
        # warn of failure before next iteration (next url).
        warn "could not get '$url': " . $response->status_line;
    }
}

# close the input file since we have finished with it.
close $fh;

# create a new template object (our output processor).
my $tp = Template->new( ) || die Template->error( );

# process the input template (input.template), passing in the values array, and write the result to the output file (output.html).
$tp->process( $input_template, { values => [ keys %values ] }, $output_file ) || die $tp->error( );

__END__

input.template は次のようになります。

<ul>
[% FOREACH value IN values %]
    <li>[% value %]</li>
[% END %]
</ul>
于 2014-03-04T00:53:32.877 に答える