用Perl下载文件 [英] Download files with Perl
问题描述
#!/ usr / bin / perl - w
使用strict;
使用LWP;
使用WWW :: Mechanize;
我的$ mech = WWW :: Mechanize-> new();
我的$ browser = LWP :: UserAgent-> new;
$ browser->凭证(
'Apache / 2.2.3(CentOS):80',
'datawww2.wxc.com',
'* ***********'=>'*************'
);
我的$ response = $ browser-> get(
'http://datawww2.wxc.com/kml/echo/MESH_Max_180min/'
);
$ mech-> follow_link(n => 8);
(原始帖子)
使用Perl下载小文件的最好方法是什么?
我查看了CPAN并发现了lwp-download,但似乎只能从链接下载。我有一个页面,链接每30分钟更改一次,日期和时间在名称中,因此它们永远不会相同。有可以使用的内置函数吗? Google上的每个人都不断地说使用Wget,但是如果可能的话,我想要坚持使用Perl,以帮助我更好地学习它。
另外有一个用户名和密码登录到该站点。我知道如何使用Perl访问网站,但我认为这可能会改变我可以使用的下载。
如您在其他问题的评论中所述:此处
您可以使用相同的方法将 .csv
文件作为 .html
或任何其他基于文本的文件。
#!/ usr / bin / perl -w
使用严格;
使用LWP :: Simple;
我的$ csv = get(http://www.spc.noaa.gov/climo/reports/last3hours_hail.csv)
或死无法获取NWS CSV页面。 ;
要登录,您可能需要使用 WWW :: Mechanize 填写webform(看看 $ mech-> get()
, $ mech-> submit_form()
和 $ mech-> follow_link() code>)
I have updated my code to look like this. When I run it though it says it cannot find the specified link. Also what is a good way to test that it is indeed connecting to the page?
#!/usr/bin/perl -w
use strict;
use LWP;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
my $browser = LWP::UserAgent->new;
$browser->credentials(
'Apache/2.2.3 (CentOS):80',
'datawww2.wxc.com',
'************' => '*************'
);
my $response = $browser->get(
'http://datawww2.wxc.com/kml/echo/MESH_Max_180min/'
);
$mech->follow_link( n => 8);
(Original Post)
What is the best way to download small files with Perl?
I looked on CPAN and found lwp-download, but it seems to only download from the link. I have a page with links that change every thirty minutes with the date and time in the name so they are never the same. Is there a built-in function I can use? Everyone on Google keeps saying to use Wget, but I was kind of wanting to stick with Perl if possible just to help me learn it better while I program with it.
Also there is a user name and password to log into the site. I know how to access the site using Perl still, but I thought that might change what I can use to download with.
As stated in a comment in your other question: here
You can use the same method to retrieve .csv
files as .html
, or any other text-based file for the matter.
#!/usr/bin/perl -w
use strict;
use LWP::Simple;
my $csv = get("http://www.spc.noaa.gov/climo/reports/last3hours_hail.csv")
or die "Could not fetch NWS CSV page.";
To login, you may need to use WWW::Mechanize to fill out the webform (look at $mech->get()
, $mech->submit_form()
, and $mech->follow_link()
)
这篇关于用Perl下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!