下载文件时出现问题

发布于 2024-09-08 16:32:36 字数 613 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

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

发布评论

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

评论(1

八巷 2024-09-15 16:32:36

抱歉,但几乎一切都是错的。

  • 您以错误的方式混合使用了LWP::UserAgentWWW::Mechanize。如果您在混合来自 2 个模块的函数时使用 $browser->get() ,则无法执行 $mech->follow_link()$mech 不知道您发出了请求。
  • 凭据参数不好,请参阅 文档

您更可能想做这样的事情:

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
$mech->get('http://datawww2.wxc.com/kml/echo/MESH_Max_180min/');
$mech->follow_link( n => 8);

您可以通过检查 $mech->success() 来检查 get() 和 follow_link() 的结果 结果
if (!$mech->success()) { warn "错误"; ... }
在 follow->link 之后,可以使用 $mech->content() 获取数据,如果要将其保存在文件中,请使用 $mech->save_content('/path /to/a/file')

完整的代码可以是:

use strict;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();

$mech->credentials( '************' , '*************'); #
$mech->get('http://datawww2.wxc.com/kml/echo/MESH_Max_180min/');
die "Error: failled to load the web page" if (!$mech->success());
$mech->follow_link( n => 8);
die "Error: failled to download content" if (!$mech->success());
$mech->save_content('/tmp/mydownloadedfile')

I'm sorry but almost everything is wrong.

  • You use a mix of LWP::UserAgent and WWW::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.
  • Arguments to credentials are not good, see the doc

You more probably want to do something like this:

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
$mech->get('http://datawww2.wxc.com/kml/echo/MESH_Max_180min/');
$mech->follow_link( n => 8);

You can check result of get() and follow_link() by checking $mech->success() result
if (!$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 :

use strict;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();

$mech->credentials( '************' , '*************'); #
$mech->get('http://datawww2.wxc.com/kml/echo/MESH_Max_180min/');
die "Error: failled to load the web page" if (!$mech->success());
$mech->follow_link( n => 8);
die "Error: failled to download content" if (!$mech->success());
$mech->save_content('/tmp/mydownloadedfile')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文