【问题标题】:Mechanize POSTing error机械化 POSTing 错误
【发布时间】:2013-10-29 14:03:07
【问题描述】:

好吧,为了启动和运行这个脚本已经花费了很长时间,现在我遇到的唯一问题是偶尔会导致它重新启动脚本的 GET 和 POST 错误,这很烦人。

sub Fight {
my($cpm);
$parsed = 0; while ($parsed == 0) {sleep(3);
$mech->get("http://www.lordsoflords.com/m3/fight_control.php");
$a = $mech->content();if ($a =~ m/Skeleton/) {$parsed = 1;}}
$mech->form_number(2);
$mech->field("Difficulty", $level);
$mech->click();
$cpm = $mech->content();
$cpm =~ m/(\<option\>208.*Duke)/;
$cpm = $1;
$cpm =~ s/ - Shadowlord Duke//g;
$cpm =~ s/\>209/\>/;
$cpm =~ s/<.*?>//g;
if($debug == 1) {
    print $cpm . "\n";
}
$mech->form_number(1);
$mech->select("Monster", $cpm);
$mech->click();
$a = $mech->content();
$a =~ m/(You win.*exp )/;
$a =~ m/(battle)/;
$a =~ m/(You have been jailed for violating our rules)/;
print $1 . "\n";
my $antal = 500 + int rand (500);
my $antal = 5000;
my $jail;

# REPEAT:
while($antal > 0) {
    sleep($loopwait); #default = 0.3
    $antal = $antal -1;
    $mech->reload();
    $a = $mech->content();
    $b = $a;
    $c = $a;

我遇到错误的一行是重复部分中的 $mech->reload() 命令,该命令返回如下所示的错误:“错误发布http://www.lordsoflords.com/m3/fight.php:booyaka.pl 行的错误请求299 英寸。

有时还会返回此错误“错误 GETing http://www.lordsoflords.com/m3/steal.php: Can't connect to www.lordsoflords.com:80 (Bad hostname) at booyaka.pl line 97”。

这个附加的函数如下:

sub Stealwait {
    $stealwait = 3600;
    $stealtime = time;
    $stealtime = $stealtime + $stealwait; # if stealer can't be found, click for 1k seconds
    print time . "|" . $stealtime . "\n";
    print "stealtime: " . $stealtime . "\n";
    $parsed = 0; $stealcount = 0;
    while ($parsed == 0) {sleep(3);
    print $stealcount . "\n";
    $mech->get("http://www.lordsoflords.com/m3/steal.php");
    $a = $mech->content();
    if ($a =~ m/Parsed/) {$parsed = 1; $stealwait = 0;}
    $stealcount = $stealcount+1; if ($stealcount == 5) {
    }
if ($a =~ m/recover/)
{
    $a = $mech->content();
    $a =~ m/(Take.*This)/s;
    $b = $1;
    $b =~ s/<.*?>//sg;
    $b =~ m/(Take.*seconds)/s;
    $b = $1;
     print $b . "\n";
    $b =~ m/(for.*seconds)/s;
    $b = $1;
    $b =~ s/for//sg;
    $b =~ s/seconds//sg;
    $b =~ s/<.*?>//sg;
    $b =~ s/,//g;
            $b = 2*$b;
            $stealwait = $b;
            print "In recover, gotta wait " . $stealwait . " seconds before I can steal...\n";
            $stealtime = time;
            $stealtime = $stealtime + $stealwait;
    }


    sub Steal {
$parsed = 0; while ($parsed == 0) {sleep(3);
$mech->get("http://www.lordsoflords.com/m3/steal.php");
$a = $mech->content();if ($a =~ m/Parsed/) {$parsed = 1;}}
    $a = $mech->content();
    if ($a =~ m/Freeplay/) { # steal only if we have freeplay
            $a = "\<option\>" . "$stealchar" . ".*?\<\/option\>";
            $tmp = $mech->content();
            #print $tmp;
            if($tmp =~ m/($a)/) {print "Stealer found\n";} else {print "Stealer not found! - not stealing!\n"; return();}
            $tmp =~ m/($a)/s;
            $tmp = $1;
            $tmp =~ s/<.*?>//sg;
            print "Stealing from: " . $tmp;
            $mech->form_name(0);
            $mech->select("Opp", $tmp);
            $mech->click_button('value' => 'Steal Stats or Items');
            $a = $mech->content();
            $a =~ m/(sleepers.*This)/s;
            $b = $1;
            $b =~ s/<.*?>//sg;
            $b =~ s/sleepers//sg;
            $b =~ s/This//sg;
            print $b;
    } else {$stealtime = time; $stealtime = $stealtime + 2000; print "Freeplay not detected, stealing cancelled...\n";}
}

在编写脚本方面我不是专家,如果我能更正此问题或覆盖 Mechanize 超时,我将不胜感激。

【问题讨论】:

  • 从实际指出 Perl 代码中的第 299 行和第 97 行开始。
  • 添加use strict; use warnings;,修复您无疑会收到的大量错误和警告,然后重新发布您的问题——如果您的问题仍然存在。大多数错误无疑与使用显式包名有关,这可以通过在使用它们的块中使用my $foo 声明变量来解决。此外,不要使用$a$b,因为它们是为sort 函数使用而保留的。
  • 299 是第一个块,$mech->reload() 行,在#REPEAT 部分,就像我在原始帖子中写的那样。 99 是第二个代码块中的第一个 $mech->get("lordsoflords.com/m3/steal.php");
  • 我最初没有写我只是想让它为我工作,整个脚本都使用严格并在其中使用警告
  • 那么脚本无疑在顶部声明了所有变量。看一眼代码,我会说它是否有效,这主要是出于旧习惯或纯粹的运气。为了知道您的 post/get 失败的原因,您需要检查 post/get 的样子。我假设您已经阅读了 Mechanize 模块的文档?

标签: php perl post get mechanize


【解决方案1】:

使用Try::Tiny 或其他东西捕获异常,稍等片刻(使用exponential back-off),然后重新发送请求。对重试的上限进行硬编码是一种常见的 Internet 礼貌,这样您就不会最终影响服务器。

【讨论】:

  • 我之前访问过该网站,我确实记得读过一些关于“退出的原因”的内容,即执行请求的速度超过每秒一次。
  • 是的,你是对的,但是这个脚本以前在内置请求上限的服务器中运行良好。为调整重新加载的等待时间的 shell 脚本设置了一个 loopwait 变量
  • 这是否与我无法找到脚本最初附带的 PXPerl 版本有关?我已经从我的包管理器中自行编译了一长串库和模块,但是我是否可能遗漏了一些东西来处理可能已经存在的这种事情?我们说的是 PXPerl 的 8 年旧版本,最初是用它编写的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-14
相关资源
最近更新 更多