我如何使用WWW :: Mechanize或任何Perl模块下载文件? [英] How can I download a file using WWW::Mechanize or any Perl module?

查看:124
本文介绍了我如何使用WWW :: Mechanize或任何Perl模块下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WWW :: Mechanize 或任何Perl模块中阅读访问网站后在文件上。例如,我点击了一个按钮'接收',一个文件(.txt)将出现,包含一条消息。我将如何能够阅读内容?答复非常感谢..我一直在这个工作了几天,而且,我尝试了所有的可能性。谁能帮忙?如果你能给我一个想法吗? :)

这是我的代码的一部分:

...

  my $ username =admin;< br> 
my $ password =12345;< br>

my $ url =http://...do_gsm_sms.cgi;

我的$机甲= WWW :: Mechanize->新建(自动检查=大于1,安静=大于0,agent_alias => $ login_agent,cookie_jar => $ cookie_jar);

$ mech->凭证($ username,$ password);< br>
$ mech-> get($ url);

$ mech->成功()或死亡无法取得请求的页面;< br>

打印OK!\\\
; #This works< br>

$ mech-> form_number(1);
$ b $ mech-> click()

;



在此之后,将出现下载对话框,以便我可以保存文件(但是我也可以设置默认打开而不是保存)。问题是,我怎样才能读取这个文件的内容?



..

解决方案

它应该),返回的数据应该存储在你的$ mech对象中。你应该能够通过 $ mech-> content()
来获取文件数据, > status()和 $ mech-> content_type()的响应类型。



您可能会发现记住WWW :: Mechanize 替换浏览器是有帮助的;浏览器所做的任何事情,比如提出一个下载窗口并保存一个文件,实际上并没有发生,但浏览器所有的信息都可以通过WWW :: Mechanize的方法获得。

Is there a way in WWW::Mechanize or any Perl module to read on a file after accessing a website. For example, I clicked a button 'Receive', and a file (.txt) will appear containing a message. How will I be able to read the content? Answers are very much appreciated.. I've been working on this for days,, Also, I tried all the possibilities. Can anyone help? If you can give me an idea please? :)

Here is a part of my code:

...

my $username = "admin";<br>
my $password = "12345";<br>

my $url = "http://...do_gsm_sms.cgi";

my $mech = WWW::Mechanize->new(autocheck => 1, quiet => 0, agent_alias =>$login_agent, cookie_jar => $cookie_jar);

$mech->credentials($username, $password);<br>
$mech->get($url);

$mech->success() or die "Can't fetch the Requested page";<br>

print "OK! \n"; #This works <br> 

$mech->form_number(1);

$mech->click()

;

After this, 'Downloads' dialog box will appear so I can save the file (but I can also set the default to open it immediately instead of saving). Question is, how can I read the content of this file?

..

解决方案

After the click (assuming that's doing what it's supposed to), the returned data should be stored in your $mech object. You should be able to get the file data with $mech->content(), perhaps after verifying success with $mech->status() and the type of response with $mech->content_type().

You may find it helpful to remember that WWW::Mechanize replaces the browser; anything a browser would have done, like bringing up a download window and saving a file, doesn't actually happen, but all the information the browser would have had is accessible through WWW::Mechanize's methods.

这篇关于我如何使用WWW :: Mechanize或任何Perl模块下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆