我正在尝试使用perl自动登录网站,然后点击链接从我的帐户获取某些数据。这里有确切的细节:
我一直陷入困境。也许我会说错了,但我想我可以使用perl LWP lib来访问表单数据并填写信息。作为第一步,我查看了www.bwsc.org上的相关源html以获取表单信息:
<div id="logonbox_SB">
<form action="/ACCOUNTS/security_main.asp" method="get">
<div id="FS_1">
<label for="AcctNum" id="acctnum_label">Account Number</label>
<input name="AcctNum" type="text" class="formtext1" size="9" maxlength="9" />
</div>
<div id="FS_2">
<label for="MtrNum" id="accsnum_label">Access Number</label>
<input name="MtrNum" type="text" class="formtext2" size="4" maxlength="4" />
</div>
<input type="image" value="Submit" name="search" class="boxsubmitbtn" src="/images/buttons/login.gif" alt="Login">
</form>
<p><a href="/ACCOUNTS/accounts.asp">Need help logging in?</a> </p>
</div><!--end logonbox_SB-->
AcctNum和MtrNum文本字段是我需要填写的。下面是我为编写这样做而编写的代码:
#! /usr/bin/perl
use strict;
use LWP::UserAgent;
use HTTP::Request::Common;
my ($ua, $request, $response, $content);
$ua = LWP::UserAgent->new;
$request = new HTTP::Request('GET','http://www.bwsc.org/ACCOUNTS/security_main.asp?AcctNum=XXX&MtrNum=XXX' );
$response = $ua->request($request);
my $content = $response->content;
open(FILE, ">out.txt");
print FILE $content;
close(FILE);
如果我查看输出文件out.txt(我认为应该包含响应html),它只是给我一个错误页面的html说我没有提供有效的帐户信息。我不确定我做错了什么。我已尝试过各种方法来传递cpan文档建议的参数,但没有运气。
有些注意事项:
答案 0 :(得分:0)
以下代码适用于我的项目
use HTTP::Headers;
use LWP::UserAgent
my $headers = HTTP::Headers->new('Accept-Encoding' => 'gzip');
my $useragent = LWP::UserAgent->new(agent => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.114 Safari/537.36', timeout => 20, keep_alive => 100, default_headers => $headers );
my $url = 'http://www.bwsc.org/ACCOUNTS/security_main.asp?AcctNum=XXX&MtrNum=XXX';
my $response = $useragent->get($url);
my $content = $response->content;
试试吧。