下载文件时出现问题
我正在尝试使用 perl 从网站下载文件。我选择不使用 wget,这样我就可以学习如何这样做。我不确定我的页面是否未连接或者我的语法是否有问题。另外,检查您是否连接到该页面的最佳方法是什么。
#!/usr/bin/perl -w
use strict;
use LWP;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
$mech->credentials( '********' , '********'); # if you do need to supply server and realms use credentials like in [LWP doc][2]
$mech->get('http://datawww2.wxc.com/kml/echo/MESH_Max_180min/');
$mech->success();
if (!$mech->success()) {
print "cannot connect to page\n";
exit;
}
$mech->follow_link( n => 8);
$mech->save_content('C:/Users/********/Desktop/');
I am trying to download a file from a site using perl. I chose not to use wget so that I can learn how to do it this way. I am not sure if my page is not connecting or if something is wrong in my syntax somewhere. Also what is the best way to check if you are getting a connection to the page.
#!/usr/bin/perl -w
use strict;
use LWP;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
$mech->credentials( '********' , '********'); # if you do need to supply server and realms use credentials like in [LWP doc][2]
$mech->get('http://datawww2.wxc.com/kml/echo/MESH_Max_180min/');
$mech->success();
if (!$mech->success()) {
print "cannot connect to page\n";
exit;
}
$mech->follow_link( n => 8);
$mech->save_content('C:/Users/********/Desktop/');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,但几乎一切都是错的。
LWP::UserAgent
和WWW::Mechanize
。如果您在混合来自 2 个模块的函数时使用$browser->get()
,则无法执行$mech->follow_link()
。$mech
不知道您发出了请求。您更可能想做这样的事情:
您可以通过检查
$mech->success() 来检查 get() 和 follow_link() 的结果
结果if (!$mech->success()) { warn "错误"; ... }
在 follow->link 之后,可以使用
$mech->content()
获取数据,如果要将其保存在文件中,请使用$mech->save_content('/path /to/a/file')
完整的代码可以是:
I'm sorry but almost everything is wrong.
LWP::UserAgent
andWWW::Mechanize
in a wrong way. You can't do$mech->follow_link()
if you use$browser->get()
as you mix function from 2 module.$mech
don't know that you did a request.You more probably want to do something like this:
You can check result of get() and follow_link() by checking
$mech->success()
resultif (!$mech->success()) { warn "error"; ... }
After follow->link, data is available using
$mech->content()
, if you want to save it in a file use$mech->save_content('/path/to/a/file')
A full code could be :