【问题标题】:Perl: Scalar Found where Operator expectedPerl:标量在运算符预期的位置找到
【发布时间】:2015-11-20 18:21:57
【问题描述】:

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

但我得到了错误:

标量在 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 编码,我想学习它并认为它适合项目。)

感谢您的时间和帮助 世界数据中心

【问题讨论】:

  • 请也开启strict
  • while ($page &lt; 4)while ($page &lt; 4);
  • 我不确定您是否在页面上获得了链接的子集或全部,但请注意 Mech 有一个 -&gt;links() 方法将返回 WWW::Mechanize 列表::将对象链接到您。您还可以使用-&gt;find_all_links() 并将条件传递给它以过滤您想要的链接。这一切都无需自己使用 HTML::Tree。
  • 谢谢,当我当前的解决方案有效时,我会试试看:)

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


【解决方案1】:

你的问题在这里:

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

因为没有 ; $elementct++ 被视为 while 条件的一部分,这不起作用。

【讨论】:

    【解决方案2】:

    你有

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

    ($page < 4) $elementct++
    

    不是有效的表达式。你的意思是写

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

    【讨论】:

    • 好的,谢谢,非常愚蠢的错误,但是 $elementct 仍然没有增加,或者它是并且 $listelement[$elementct] 只是不工作。你也有这个想法?
    • 始终使用use strict; use warnings;。这会发现你的错误。
    【解决方案3】:

    致命错误

    • 正如其他人所指出的,您需要在 do { ... } while ... 循环的末尾使用分号 ;

    • $elementct 存在范围界定问题,如果您按照应有的方式设置了 use strict,就会发现该问题。您正在尝试在包含其声明的块之外增加变量

    最佳实践指南

    • 在您编写的每个程序的顶部同时包含use strictuse warnings(优先于shebang 行上的-w)是至关重要的

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

    • 您应该使用词法文件句柄而不是全局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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-16
      相关资源
      最近更新 更多