从HTML中提取文本(Perl)

时间:2010-07-06 16:08:00

标签: perl http tree parsing match

我正在做一个进入页面并从中提取和提取信息的脚本。 我在Perl中编写的脚本。

问题:不是如何开始运行脚本,因为当我开始它时会选择这样的URL,这不是我想要的

<a href="http://valeptr.com/scripts/runner.php?BA=6672&amp;hash=08c5c66839a468a11b7574e6ce02e0&amp;url=http%3A%2F%2Fdizzydollarsgpt.com%2Fmembers%2Fregister.php%3Fref%3Dthomasd24" target="_blank"><img alt="DizzyDollarsGPT" border="0" src="enter.php_files/runner.jpeg" /></a>

我希望得到这个:

<a href="http://valeptr.com/scripts/runner.php?PA=33425" target="_ptc" onclick="javascript:reloadpage(11)"> <img src="1appsearch.php_files/runner_007.gif" alt="Xray-cash" border="0">

所有代码都在这里:

#!/usr/bin/perl
#=======================================================================
#
# FILE: ValePTR.pl
#
# USAGE: ./ValePTR.pl user password
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: libgetopt-declare-perl
# BUGS: ---
# NOTAS: ---
# AUTOR: Alejandro
# VERSION: 1.0
# CREATED: Lunes 5 de julio del 2010
# REVISION: 1
#=======================================================================

use warnings;
use strict;
use HTML::TreeBuilder;
use WWW::Mechanize;
use Getopt::Long;
my($content, $search_result, @search_results);

    #Constructor del explorador con un UserAgent falso.
    my $Explorador = WWW::Mechanize->new( agent => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624' );
    $Explorador->get("file://home/alejandro/enter.php.html"); #Se procede a acceder a la dirección url para hacer el HTTP Post
    #$Explorador->field('username','miuser'); # Busca el campo username y pone el usuario
    #$Explorador->field('password','mipass'); # Busca el campo password y pone la contraseña
    #$Explorador->submit(); # Hace el HTTP POST

        #print $Explorador->content();
#parse $content with treebuilder
my $page = HTML::TreeBuilder->new();
$page->parse($Explorador->content());
$page->eof();


@search_results= $page->look_down(
sub{ $_[0]-> tag() eq 'a' and ($_[0]->attr('href'))}
);

foreach $search_result (@search_results){
my($url, $title, $summary);

$title = $page->look_down(
sub{ $_[0]-> tag() eq 'a' and ($_[0]->attr('href'))}
);
if($title)
{
print 'title: '.$title->as_HTML,"\n";
}
}


$page->delete;

所有HTML代码都在这里:http://gist.github.com/465568

PD:请帮助我,我已经在这里待了3个小时没有成功

最终会发生的事情就是把所有东西都拿出来

http://valeptr.com/scripts/runner.php?BA=

我想要的是:

http://valeptr.com/scripts/runner.php?PA=

2 个答案:

答案 0 :(得分:1)

您对look_down()的来电无法区分您想要的链接和您不想要的链接。尝试更强大的过滤器,如

@search_results = $page->look_down(
    sub {$_[0]->{tag} eq 'a'  &&
         $_[0]->attr('href') =~ /\?PA=/}); # only match http://...?PA=...

答案 1 :(得分:1)

为了避免构建文档树的开销,我倾向于使用HTML::TokeParser::Simple

#!/usr/bin/perl

use strict; use warnings;

use HTML::TokeParser::Simple;

my $parser = HTML::TokeParser::Simple->new('t.html');

while ( my $tag = $parser->get_tag('a') ) {
    my $href = $tag->get_attr('href');
    next unless $href =~ /runner\.php\?PA=[0-9]+\z/;

    print $tag->as_is;

    while ( my $token = $parser->get_token ) {
        print $token->as_is;
        last if $token->is_end_tag('/a');
    }
    print "\n";
}

输出:

<a href="http://valeptr.com/scripts/runner.php?PA=33425" target="_ptc" onclick="javascript:reloadpage(11)"> <img src="1appsearch.php_files/runner_007.gif" alt="Xray-cash" border="0"> </a> ... etc