Perl WWW::Mechanize-Grab<;表格>;要传递给$url2的变量的操作
多年来,我一直在使用WWW-Mechanize登录页面,提交表单数据,然后抓取数据/显示数据等。现在的问题是,我抓取的源站点在登录后开始使用另一个变量(POST数据),这是一个随机的动态URL(这是与我一起登录的人的用户ID,似乎正在更改。)我似乎找不到方法获取操作链接以将其存储为变量:$url2Perl WWW::Mechanize-Grab<;表格>;要传递给$url2的变量的操作,perl,Perl,多年来,我一直在使用WWW-Mechanize登录页面,提交表单数据,然后抓取数据/显示数据等。现在的问题是,我抓取的源站点在登录后开始使用另一个变量(POST数据),这是一个随机的动态URL(这是与我一起登录的人的用户ID,似乎正在更改。)我似乎找不到方法获取操作链接以将其存储为变量:$url2 <form name="searchForm" method="post" action="../Emsresponse/?id=106" onsubmit="return validate(
<form name="searchForm" method="post" action="../Emsresponse/?id=106" onsubmit="return validate()">
我只需要将数据作为变量存储在action中(即:../Emsresponse/?id=106),但我找不到可靠的方法来使用Mechanize实现这一点
所以我需要$url2来包含?id=现在是什么,这样我就不必每两周修改一次代码
$url = "www.page.com";
$url2 = "www.page.com/userName/id?=101";
my $r = $mech->get($url2);
# if we are not logged in $r hold has a key previous holding the http response from the 302 redirect call to the login page
if (1) {
# if we were redirected to the login location we are not logged in
if (1) {
$mech->get($url);
if ( $mech->success() ) {
$mech->form_name("loginForm");
$mech->set_fields( LoginUserName => ... );
$mech->click();
if ( $mech->success() ) {
$authme = 0;
}
# check for success --> this is important to not break the script on any errors
if ( $authme == 0
&& $mech->success() )
{
$mech->cookie_jar->save($cookie_file);
$mech->form_name("searchForm");
$mech->set_fields( DateStart => $date_yesterday );
$mech->click();
if ( $mech->success() ) { }
似乎是服务器端问题导致了此问题。您的问题是什么?我不明白您试图做什么。请回答您的问题,并显示您在WWW::Mechanize中使用的实际代码,并说明您试图实现的目标。
$url
在您的程序中来自哪里?什么是$url2
你为什么需要它?你想对这两个URL做什么?你的代码做什么?你是否从HTML中解析出动作,然后提交另一个请求?你为什么不使用Mechanize来填写表单并提交它?它将使用正确的动作,除非它是通过JS完成的(在这种情况下,解析它也没有帮助)是的,我通过抓取来解析HTML页面。这是两个不同的表单请求,一个用于登录,一个用于数据/数据/时间。我已经添加了正在生产的代码部分。我必须用if(1)来包装它们因为服务器取消了3xx重定向,并使用了看起来可以正常工作的动态URLST。添加调试以查看loginForm
Mechanize看到的操作。此外,该代码中没有删除。
$url = "www.page.com";
$url2 = "www.page.com/userName/id?=101";
my $r = $mech->get($url2);
# if we are not logged in $r hold has a key previous holding the http response from the 302 redirect call to the login page
if (1) {
# if we were redirected to the login location we are not logged in
if (1) {
$mech->get($url);
if ( $mech->success() ) {
$mech->form_name("loginForm");
$mech->set_fields( LoginUserName => ... );
$mech->click();
if ( $mech->success() ) {
$authme = 0;
}
# check for success --> this is important to not break the script on any errors
if ( $authme == 0
&& $mech->success() )
{
$mech->cookie_jar->save($cookie_file);
$mech->form_name("searchForm");
$mech->set_fields( DateStart => $date_yesterday );
$mech->click();
if ( $mech->success() ) { }