开发者

WWW:Mechanize Form Select

I am attempting to login to Youtube with WWW:Mechanize and use forms() to print out all the forms on the page after logging in. My script is logging in successfully, and also successfully navigating to Youtube.com/inbox; However, for some reason Mechanize can not see any forms at Youtube.com/inbox. It just returns blank. Here is my code:

#!"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&ltmpl=sso';

my $mechanize = WWW::Mechanize->new(autocheck => 1);

$mech开发者_高级运维anize->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 is unable to see any forms at youtube.com/inbox, however, like I said, I can print all of the forms from the initial link, no matter what I change it to...

Thanks in advance.


As always, one of the best debugging approaches is to print what you get and check if it is what you were expecting. This applies to your problem too.

In your case, if you print $mechanize->content() you'll see that you didn't get the page you're expecting. YouTube wants you to follow a JavaScript redirect in order to complete your cross-domain login action. You have multiple options here:

  • parse the returned content manually – i.e. /location\.replace\("(.+?)"/
  • try to have your code parse JavaScript (have a look at WWW::Scripter)
  • [recommended] use YouTube API for managing your inbox
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜