【发布时间】: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 < 4)→while ($page < 4); -
我不确定您是否在页面上获得了链接的子集或全部,但请注意 Mech 有一个
->links()方法将返回 WWW::Mechanize 列表::将对象链接到您。您还可以使用->find_all_links()并将条件传递给它以过滤您想要的链接。这一切都无需自己使用 HTML::Tree。 -
谢谢,当我当前的解决方案有效时,我会试试看:)
标签: perl fetch operator-keyword www-mechanize scraper