Perl:Scalar找到了运营商的预期

时间:2015-08-26 15:52:19

标签: perl fetch operator-keyword www-mechanize scraper

我目前正在为另一个项目编写一个小型Google-Scraper。

但是我得到了错误:

  

Scalar找到了运营商在test.pl第50行所预期的位置,靠近")   $ elementct(在$ elementct之前缺少运算符?)

     

test.pl第50行的语法错误,接近")$ elementct"

     

由于编译错误,test.pl的执行中止。

#!/usr/bin/perl -w
use WWW::Mechanize;
use HTML::Parser;
use HTML::Tree;
use warnings;

open (F, "testlist.txt") || die "Could not open test.txt: $!\n";
my @listelement = <F>;
close F;

do {
    my $elementct  = 0;
    my $donk = $listelement[$elementct];     
    my $bot = WWW::Mechanize -> new(); 
    $bot -> agent_alias ('Windows IE 6'); 
    $google = "http://www.google.com/search?q=yoursearch+";
    $search_url = "$google $donk";
    $bot -> get ($search_url);

    my $page = 1;

    do {
        my $tree = HTML::Tree -> new();

        $tree -> parse ($bot -> content);

        #Store the Links in an array
        my @link = $tree -> look_down ('_tag','cite');

        # Check if we got something, exit otherwise
        if (!@link)
        {
            print "\nERROR NO RESULTS\n\n";
            exit 1;
        }

        # Print the results per page
        print "\nResults from Page $page\n";
        for my $url (@link)
        {
            print "|__ ".$url -> as_HTML."\n";
        }
        $page++;
        # increment page numbers
        $bot -> follow_link (text => $page);
        print  "\n";
        sleep 3;

    } while ($page < 4)
    $elementct++
} while ($elementct < scalar @listelement)

并且在阅读了很多类似的问题之后,我不知道我要做些什么来修复它。

(我第一次使用perl进行编码,我想学习它,并认为它适合项目。)

感谢您的时间和帮助 WDC

3 个答案:

答案 0 :(得分:2)

你有

do { ... } while ($page < 4) $elementct++;

($page < 4) $elementct++

不是有效的表达式。你打算写

do { ... } while ($page < 4); $elementct++;

答案 1 :(得分:1)

你的问题在这里:

    } while ($page < 4)
    $elementct++
    } while ($elementct < scalar @listelement)

因为没有; $elementct++被视为while条件的一部分,这是无效的。

答案 2 :(得分:0)

致命错误

  • 正如其他人所说,在;循环结束时需要使用分号do { ... } while ...

  • 您对$elementct存在范围问题,如果您有use strict,则会显示该问题。您试图在包含其声明

  • 的块之外增加变量

最佳做法指南

  • 在每个程序的顶部同时拥有use strictuse warnings(优先于shebang行上的-w至关重要你写了

  • do { ... } while ...形式的循环很少是你想要的,因为无论循环条件如何,它总是至少执行一次循环体。使用更标准的while (...) { ... },除非您确定需要更特别的内容

  • 您应该使用 lexical 文件句柄而不是全局F,以及open

  • 的三参数形式

您的代码看起来应该是这样的

#!/usr/bin/perl

use strict;
use warnings;

use WWW::Mechanize;
use HTML::Parser;
use HTML::Tree;

my @listelement;
{
    open my $fh, '<', "testlist.txt" or die "Could not open test.txt: $!";
    @listelement = <$fh>;
    chomp @listelement;
}

my $elementct = 0;

while ( $elementct < @listelement ) {

    my $elementct  = 0;
    my $donk       = $listelement[$elementct];
    my $bot        = WWW::Mechanize->new();
    my $google     = "http://www.google.com/search?q=yoursearch+";
    my $search_url = "$google $donk";

    $bot->agent_alias( 'Windows IE 6' );
    $bot->get( $search_url );

    my $page = 1;

    while ( $page < 4 ) {

        my $tree = HTML::Tree->new;

        $tree->parse( $bot->content );

        #Store the Links in an array
        my @link = $tree->look_down( '_tag', 'cite' );

        # Check if we got something, exit otherwise
        if ( !@link ) {
            print "\nERROR NO RESULTS\n\n";
            exit 1;
        }

        # Print the results per page
        print "\nResults from Page $page\n";
        for my $url ( @link ) {
            print "|__ " . $url->as_HTML . "\n";
        }
        ++$page;

        # increment page numbers
        $bot->follow_link( text => $page );
        print "\n";
        sleep 3;

    }

    ++$elementct;
}