我正在尝试使用WWW:Mechanize登录Youtube,并在登录后使用forms()打印出页面上的所有表单。我的脚本登录成功,也成功导航到Youtube.com/inbox;但是,由于某种原因,Mechanize在Youtube.com/inbox上看不到任何表单。它只是返回空白。下面是我的代码:
#!"C:\Perl64\bin\perl.exe" -T
use strict;
use warnings;
use CGI;
use CGI::Carp qw/fatalsToBrowser/;
use WWW::Mechanize;
use Data::Dumper;
my $q = CGI->new;
$q->header();
my $url = 'https://www.google.com/accounts/ServiceLogin?uilel=3&service=youtube&passive=true&continue=http://www.youtube.com/signin%3Faction_handle_signin%3Dtrue%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252Findex&hl=en_US<mpl=sso';
my $mechanize = WWW::Mechanize->new(autocheck => 1);
$mechanize->agent_alias( 'Windows Mozilla' );
$mechanize->get($url);
$mechanize->submit_form(
form_id => 'gaia_loginform',
fields => { Email => 'myemail',Passwd => 'mypassword' },
);
die unless ($mechanize->success);
$url = 'http://www.youtube.com/inbox';
$mechanize->get($url);
$mechanize->form_id('comeposeform');
my $page = $mechanize->content();
print Dumper($mechanize->forms());Mechanize无法在youtube.com/inbox上看到任何表单,但是,正如我所说,我可以打印初始链接中的所有表单,无论我将其更改为什么……
提前谢谢。
发布于 2011-02-04 17:34:02
一如既往,最好的调试方法之一就是print你得到的东西,并检查它是否是你所期望的。这也适用于你的问题。
在你的例子中,如果你使用print $mechanize->content(),你会发现你没有得到你想要的页面。YouTube希望您遵循JavaScript重定向,以完成跨域登录操作。这里有多个选项:
使用手动解析返回的内容-例如,使用/location\.replace\("(.+?)"/
https://stackoverflow.com/questions/4893625
复制相似问题