为什么 Perl 的 WWW::Mechanize 不能通过字段名称找到表单?

发布于 2024-08-30 03:34:31 字数 2160 浏览 3 评论 0原文

#!/usr/bin/perl

use WWW::Mechanize;
use Compress::Zlib;

my $mech = WWW::Mechanize->new();

my $username = ""; #fill in username here
my $keyword = "";  #fill in password here

my $mobile = $ARGV[0];
my $text = $ARGV[1];

$deb = 1;

print length($text)."\n" if($deb);

$text = $text."\n\n\n\n\n" if(length($text) < 135);

$mech->get("http://wwwl.way2sms.com/content/index.html");
unless($mech->success())
{
 exit;
}
$dest = $mech->response->content;

print "Fetching...\n" if($deb);

if($mech->response->header("Content-Encoding") eq "gzip")
{
 $dest = Compress::Zlib::memGunzip($dest);
 $mech->update_html($dest);
}

$dest =~ s/<form name="loginForm"/<form action='..\/auth.cl' name="loginForm"/g;


$mech->update_html($dest);
$mech->form_with_fields(("username","password"));
$mech->field("username",$username);
$mech->field("password",$keyword);

print "Loggin...\n" if($deb);

$mech->submit_form();

$dest= $mech->response->content;

if($mech->response->header("Content-Encoding") eq "gzip")
{
        $dest = Compress::Zlib::memGunzip($dest);
        $mech->update_html($dest);
}

$mech->get("http://wwwl.way2sms.com//jsp/InstantSMS.jsp?val=0");
$dest= $mech->response->content;
if($mech->response->header("Content-Encoding") eq "gzip")
{
        $dest = Compress::Zlib::memGunzip($dest);
        $mech->update_html($dest);
}

print "Sending ... \n" if($deb);

$mech->form_with_fields(("MobNo","textArea"));
$mech->field("MobNo",$mobile);
$mech->field("textArea",$text);
$mech->submit_form();

if($mech->success())
{
print "Done \n" if($deb);
}
else
{
print "Failed \n" if($deb);
exit;
}

$dest =  $mech->response->content;
if($mech->response->header("Content-Encoding") eq "gzip")
{
        $dest = Compress::Zlib::memGunzip($dest);
  #print $dest if($deb);
}

if($dest =~ m/successfully/sig)
{
  print "Message sent successfully" if($deb);
}

exit;

运行时,此代码停止并显示错误:

./sms.pl 第 65 行没有包含所请求字段的表单
无法对 /usr/share/perl5/vendor_perl/WWW/Mechanize.pm 第 1348 行的未定义值调用方法“value”。

#!/usr/bin/perl

use WWW::Mechanize;
use Compress::Zlib;

my $mech = WWW::Mechanize->new();

my $username = ""; #fill in username here
my $keyword = "";  #fill in password here

my $mobile = $ARGV[0];
my $text = $ARGV[1];

$deb = 1;

print length($text)."\n" if($deb);

$text = $text."\n\n\n\n\n" if(length($text) < 135);

$mech->get("http://wwwl.way2sms.com/content/index.html");
unless($mech->success())
{
 exit;
}
$dest = $mech->response->content;

print "Fetching...\n" if($deb);

if($mech->response->header("Content-Encoding") eq "gzip")
{
 $dest = Compress::Zlib::memGunzip($dest);
 $mech->update_html($dest);
}

$dest =~ s/<form name="loginForm"/<form action='..\/auth.cl' name="loginForm"/g;


$mech->update_html($dest);
$mech->form_with_fields(("username","password"));
$mech->field("username",$username);
$mech->field("password",$keyword);

print "Loggin...\n" if($deb);

$mech->submit_form();

$dest= $mech->response->content;

if($mech->response->header("Content-Encoding") eq "gzip")
{
        $dest = Compress::Zlib::memGunzip($dest);
        $mech->update_html($dest);
}

$mech->get("http://wwwl.way2sms.com//jsp/InstantSMS.jsp?val=0");
$dest= $mech->response->content;
if($mech->response->header("Content-Encoding") eq "gzip")
{
        $dest = Compress::Zlib::memGunzip($dest);
        $mech->update_html($dest);
}

print "Sending ... \n" if($deb);

$mech->form_with_fields(("MobNo","textArea"));
$mech->field("MobNo",$mobile);
$mech->field("textArea",$text);
$mech->submit_form();

if($mech->success())
{
print "Done \n" if($deb);
}
else
{
print "Failed \n" if($deb);
exit;
}

$dest =  $mech->response->content;
if($mech->response->header("Content-Encoding") eq "gzip")
{
        $dest = Compress::Zlib::memGunzip($dest);
  #print $dest if($deb);
}

if($dest =~ m/successfully/sig)
{
  print "Message sent successfully" if($deb);
}

exit;

When run this code halts with an error saying:

There is no form with the requested fields at ./sms.pl line 65
Can't call method "value" on an undefined value at /usr/share/perl5/vendor_perl/WWW/Mechanize.pm line 1348.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸵鸟症 2024-09-06 03:34:31

我猜想没有 带有字段的表单“MobNo”& http://wwwl.way2sms.com//jsp/InstantSMS.jsp?val=0 中的“textArea”确实不存在,因为该 URL 的页面甚至缺少 < ;body> 标签。

I would guess that there is no form with the fields "MobNo" & "textArea" in http://wwwl.way2sms.com//jsp/InstantSMS.jsp?val=0 which indeed there aren't since the page at that URL lacks even a <body> tag.

沙与沫 2024-09-06 03:34:31
$dest =~ s/<form name="loginForm"/<form action='..\/auth.cl' name="loginForm"/g;

在脚本中找到上面的行并将其替换为以下内容

$dest =~ s/<form name="loginForm"/<form action='..\/Login1.action' name="loginForm"/ig; 

这是必需的,因为最近 way2sms 重组了其主页,因此 auth.cl 表单已重命名为 Login1.action

$dest =~ s/<form name="loginForm"/<form action='..\/auth.cl' name="loginForm"/g;

find the above line in the script and replace it with the following

$dest =~ s/<form name="loginForm"/<form action='..\/Login1.action' name="loginForm"/ig; 

This is required since recently way2sms has restructured its home page and hence the auth.cl form was renamed to Login1.action

归属感 2024-09-06 03:34:31

当我遇到这类问题时,我会打印整个 HTML 页面,以便查看。您期待的表格可能不存在。我怀疑您没有获得您认为的页面。

第一页执行大量 JavaScript 处理来提交表单。由于 WWW::Mechanize 不会为您处理任何这些位,我'我猜测您的第一个表单提交在某种程度上不完整或无效,因此您看到的下一页是某种错误页面。这是动态网站的一个非常常见的问题。

您还可以将 Mech 的功能与支持 JavaScript 的浏览器的功能进行比较。使用某种 HTTP 嗅探工具来监视事务。交互式浏览器是否做了一些 Mech 没有做的额外事情?

When I run into these sorts of problems, I print the entire HTML page so I can look at it. The form you are expecting probably isn't there. I suspect that you aren't getting the page that you think you are.

The first page does quite a bit of JavaScript processing to submit the form. Since WWW::Mechanize doesn't handle any of those bits for you, I'm guessing that your first form submission is somehow incomplete or invalid, so the next page you get is some sort of error page. That's a quite common problem with dynamic websites.

You can also compare what Mech does with what a JavaScript-enabled browser does. Use some sort of HTTP sniffing tool to watch the transactions. Is the interactive browser doing something extra that Mech isn't?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文