WWW:机械化表格选择

发布于 2024-10-16 05:08:22 字数 1250 浏览 4 评论 0原文

我正在尝试使用 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&ltmpl=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 上看到任何表单,但是,就像我说的,我可以从初始链接打印所有表单,无论我将其更改为什么...

提前致谢。

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<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 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.

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

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

发布评论

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

评论(1

臻嫒无言 2024-10-23 05:08:22

与往常一样,最好的调试方法之一是打印您得到的内容并检查它是否是您所期望的。这也适用于你的问题。

在您的情况下,如果您 print $mechanize->content() 您会发现您没有获得您期望的页面。 YouTube 希望您遵循 JavaScript 重定向来完成跨域登录操作。您在这里有多个选项:

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