如何使用Perl登录YouTube?

如何使用Perl登录YouTube?,perl,screen-scraping,youtube,Perl,Screen Scraping,Youtube,我正试图编写一个Perl脚本来连接我的YouTube帐户,但它似乎不起作用。基本上,我只是想连接到我的帐户,但显然它不工作。我甚至不知道如何调试它!也许它与https协议有关 请开导我!提前谢谢 use HTTP::Request::Common; use LWP::UserAgent; use strict; my $login="test"; my $pass = "test"; my $res = ""; my $ua = ""; # Create user agent, make i

我正试图编写一个Perl脚本来连接我的YouTube帐户,但它似乎不起作用。基本上,我只是想连接到我的帐户,但显然它不工作。我甚至不知道如何调试它!也许它与https协议有关

请开导我!提前谢谢

use HTTP::Request::Common;
use LWP::UserAgent;
use strict;

my $login="test";
my $pass = "test";
my $res = "";
my $ua = "";

# Create user agent, make it look like FireFox and store cookies
$ua = LWP::UserAgent->new;
$ua->agent("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20051213 Firefox/1.0.7");
$ua->cookie_jar ( {} );

# Request login page 
$res = $ua->request(GET "https://www.google.com/accounts/ServiceLogin?service=youtube&hl=en_US&passive=true&ltmpl=sso&uilel=3&continue=http%3A//www.youtube.com/signup%3Fhl%3Den_US%26warned%3D%26nomobiletemp%3D1%26next%3D/index");
die("ERROR1: GET http://www.youtube.com/login\n") unless ($res->is_success);


# Now we login with our user/pass
$res = $ua->request(
        POST "https://www.google.com/accounts/ServiceLoginAuth?service=youtube",
        Referer => "http://www.youtube.com/login",
        Content_Type => "application/x-www-form-urlencoded",
        Content => [
                currentform     => "login",
                next            => "/index",
                username        => $login,
                password        => $pass,
                action_login    => "Log+In"
        ]
        );

# YouTube redirects (302) to a new page when login is success
# and returns OK (200) if the login failed.
#die("ERROR: Login Failed\n") unless ($res->is_redirect());


print $res->content;

我正在学习perl的web特性,所以我不想使用除wwwlib或mechanize之外的任何库来完成这项工作。 如何使用perl脚本连接到我的帐户?这是我目前的目标 希望有人可以发布一个脚本或更正我的。 谢谢你们的帮助。
我现在正在测试Webscarab..

您想获取哪些数据?为什么不使用现有的实现,比如

关于您的代码的一些评论:我总是避免使用快捷方式
$ua->request(GET/POST)
方法,因为我总是需要更多的灵活性,而只有使用和才允许。我总是觉得这样的代码更干净

为什么你的代码不起作用?谁知道呢。 确保您的cookiejar正在将您的Cookie添加到传出的文件中。我建议在浏览器中执行此操作时转储所有标题,并与发送的标题和数据进行比较。他们可能会检查一些额外的字段,这些字段在每次点击时都会有所不同。他们可能正在检查您的
UserAgent
字符串。如果你只是想学习,我建议你使用不同的网站作为目标,因为我确信YouTube有各种各样的反脚本强化功能。

你在使用什么

使用HTTP代理,如监视数据流


Trey建议将其他人的CPAN软件包用于机械设备也是一个好主意。

大体上,您要做的是为大多数具有重定向登录的网站定义一个cookiejar。这就是一揽子计划所做的。此外,该软件包还根据youtube规范调整了许多查找和刮取

例如,Ajax内容将是粗糙的,因为它在您的抓取时不存在

你刚选了一个有点粗糙的页面开始


享受

我自己也在研究这个问题。在此之前,我建议把谷歌的这篇文章作为一个很好的开始参考。如果我读得正确,首先通过REST接口传递用户凭据以获得身份验证令牌。为了解决这个问题,我使用了以下方法:

sub getToken {
  my %parms = @_;
  my $response    = LWP::UserAgent->new->post(
                   'https://www.google.com/youtube/accounts/ClientLogin',
                   [
                         Email   => $parms{'username'},
                         Passwd  => $parms{'password'},
                         service => "youtube",
                         source  => "<<Your Value Here>>",                            
                   ]
    );


    my $content = $response->content;


    my ($auth) = $content =~ /^Auth=(.*)YouTubeUser(.*)$/msg
           or die "Unable to authenticate?\n";
    my ($user) = $content =~ /YouTubeUser=(.*)$/msg
            or die "Could not extract user name from response string. ";

    return ($auth, $user);
}
一旦我有了这两个东西--$AuthToken和$GoogleUserName,我仍然在测试LWP帖子。我还在写这个单元:

sub test {

my %parms = @_;

## Copy file contents. Use, foy's three param open method. 
my $fileSize = -s $parms{'File'};
open(VideoFile, '<', "$parms{'File'}") or die "Can't open $parms{'File'}.";
binmode VideoFile;
read(VideoFile, my $fileContents, $fileSize) or die "Can't read $parms{'File'}";
close VideoFile;




my $r = LWP::UserAgent->new->post(
    "http://uploads.gdata.youtube.com/feeds/api/users/$parms{'user'}/uploads",
    [
        Host              => "uploads.gdata.youtube.com",
        'Authorization'     => "AuthSub token=\"$parms{'auth'}\"",
        'GData-Version'     => "2",
        'X-GData-Key'       => "key=$YouTubeDeveloperKey",
        'Slug'              => "$parms{'File'}",
        'Content-Type'      => "multipart/related; boundary=\"<boundary_string>\"",
        'Content-Length'    => "<content_length>",
        'video_content_type'=> "video/wmv",
        'Connection'        => "close",
        'Content'           => $fileContents
    ]

);


print Dumper(\$r->content)
}
sub test {

my %parms = @_;

## Copy file contents. Use, foy's three param open method. 
my $fileSize = -s $parms{'File'};
open(VideoFile, '<', "$parms{'File'}") or die "Can't open $parms{'File'}.";
binmode VideoFile;
read(VideoFile, my $fileContents, $fileSize) or die "Can't read $parms{'File'}";
close VideoFile;




my $r = LWP::UserAgent->new->post(
    "http://uploads.gdata.youtube.com/feeds/api/users/$parms{'user'}/uploads",
    [
        Host              => "uploads.gdata.youtube.com",
        'Authorization'     => "AuthSub token=\"$parms{'auth'}\"",
        'GData-Version'     => "2",
        'X-GData-Key'       => "key=$YouTubeDeveloperKey",
        'Slug'              => "$parms{'File'}",
        'Content-Type'      => "multipart/related; boundary=\"<boundary_string>\"",
        'Content-Length'    => "<content_length>",
        'video_content_type'=> "video/wmv",
        'Connection'        => "close",
        'Content'           => $fileContents
    ]

);


print Dumper(\$r->content)
}
&test((auth=>$Auth, user=>$user, File=>'test.wmv'));