如何使用 Perl 登录 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 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<mpl=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..
I am trying to write a Perl script to connect to me YouTube account but it doesnt seem to work. Basically I just want to connect to my account but apparently it is not working. I don't even have an idea on how I could debug this! Maybe it is something related to https protocol?
Please enlighten me! Thanks in advance.
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<mpl=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;
what i am doing is learning the web features of perl, so i dont want to use any library except wwwlib or mechanize to get the job done.
how can i just connect to my account using a perl script? this is my objective for now
hope someone can post a script or correct mine.
thank you guys for you help.
i am testing Webscarab now..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你想抓取什么数据? 为什么不直接使用现有的实现,例如
WebService::YouTube
对您的代码的一些评论:我总是避免使用快捷方式
$ua->request(GET/POST)
方法,因为我总是最终需要比仅使用HTTP::Request
和HTTP::Response
允许。 我总觉得这样代码也更干净。为什么你的代码不起作用? 谁知道。
确保您的 cookiejar 将您的 cookie 添加到传出
HTTP::Request
。 我建议您在浏览器中执行此操作时转储所有标头,并与libwww
正在发送。 他们可能正在检查一些附加字段,这些字段每次点击都会有所不同。 他们可能正在检查您的UserAgent
字符串。 如果您只是想学习libwww
我'我建议使用不同的网站作为目标,因为我确信 YouTube 具有各种反脚本强化功能。What data are you trying to grab? Why not just using an existing implementation like
WebService::YouTube
Some comments on your code: I always avoided the shortcut
$ua->request(GET/POST)
method since I always ended up needing more flexibility that only the use ofHTTP::Request
andHTTP::Response
allowed. I always felt the code was cleaner that way too.Why is your code not working? Who knows.
Make sure your cookiejar is adding your cookies to the outgoing
HTTP::Request
. I'd suggest dumping all your headers when you do it in a browser and compare with the headers and data thatlibwww
is sending. There may be some additional fields that they are checking for that vary for every hit. They may be checking for yourUserAgent
string. If you are just looking to learnlibwww
I'd suggest using a different site as a target as I'm sure YouTube has all sort of anti-scripting hardening.您是否使用 YouTube 的稳定记录 API?
使用 HTTP 代理(例如 WebScarab)来监视数据流。
Trey 建议使用其他人的 CPAN 包作为机制也是一个好主意。
Are you using YouTube's stable documented API?
Use an HTTP proxy such as WebScarab to watch the data flow.
Trey's suggestion to use somebody else's CPAN package for the mechanics is a good idea too.
总的来说,您想要做的就是为大多数具有重定向登录的网站定义一个 cookiejar。 这就是该包所做的。 此外,该软件包还根据 YouTube 规范调整了许多查找和抓取。
例如,Ajax 内容将很粗糙,因为当您刚开始抓取时,它不存在,
您只是选择了一个有点粗糙的页面。
享受
Right right by and large, what you want to do is define a cookiejar for most of these websites that have a redirection login. This is what the package has done. Also the package tunes a lot of the lookups and scrapes based on the youtube spec.
Ajax content for example will be rough since its not there when your scraping
You just picked a somewhat rough page to start out with.
Enjoy
我自己实际上也在研究这个问题。 在此之前,我建议您阅读一下来自的 API 指南谷歌是一个很好的起始参考。 如果我没看错的话,首先会通过 REST 接口传递用户凭据来获取身份验证令牌。 为了处理这个问题,我使用以下内容:
我从程序的主要部分调用它:
一旦我有了这两个东西——$AuthToken和$GoogleUserName,我仍在测试LWP Post。 我仍在写这个单元:
这就是所谓的
I'm actually working on this issue myself. Before, I would suggest read over this the API guide from Google as a good starting reference. If I'm reading it correctly, one begins with passing user credentials through a REST interface to get a Authentication Token. To handle that, I'm using the following:
And I call that from the main part of my program as such:
Once I have these two things -- $AuthToken and $GoogleUserName, I'm still testing the LWP Post. I'm still writing this unit:
And that is called as