Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 如何在WWW::Mechanize中获取后续链接的内容?_Perl_Www Mechanize_Download - Fatal编程技术网

Perl 如何在WWW::Mechanize中获取后续链接的内容?

Perl 如何在WWW::Mechanize中获取后续链接的内容?,perl,www-mechanize,download,Perl,Www Mechanize,Download,我希望这是我的最后一个问题。我正在使用$mech->follow_链接尝试下载一个文件。出于某种原因,虽然保存的文件只是我第一次打开的页面,而不是我想要跟随的链接。这是从链接下载文件的正确方式吗?我不想使用wget #!/usr/bin/perl -w use strict; use LWP; use WWW::Mechanize; my $now_string = localtime; my $mech = WWW::Mechanize->

我希望这是我的最后一个问题。我正在使用$mech->follow_链接尝试下载一个文件。出于某种原因,虽然保存的文件只是我第一次打开的页面,而不是我想要跟随的链接。这是从链接下载文件的正确方式吗?我不想使用wget

    #!/usr/bin/perl -w
    use strict;
    use LWP;
    use WWW::Mechanize;
    my $now_string = localtime;
    my $mech = WWW::Mechanize->new();
    my $filename = join(' ', split(/\W++/, $now_string, -1));
    $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/') or die "Error: failed to load the web page";
$mech->follow_link( url_regex => qr/MESH/i ) or die "Error: failed to download content";
$mech->save_content("$filename.kmz");

if
更改为
,除非

您确定要尝试第三个名为“MESH”的链接吗?

步骤
  • 首先打印
    get
    中的内容,以确保到达的是有效的HTML页面
  • 确保您要创建的链接是第三个名为“网格”的链接(区分大小写?)
  • 打印第二张
    get
  • 打印文件名以确保其格式正确
  • 检查文件是否已成功创建
  • 附加的
    • 除非在任何一种情况下——它会起作用,或者它会死,否则你都不需要它

    例子
    将“如果”更改为“除非”。如何打印内容?我试着打印$mech->content(格式=>'text');但是它似乎不起作用。你可以使用
    print$mech->response()->content()
    print$mech->content()
    ,甚至
    print%{$mech->get($url)}
    format=>'text'
    将剥离HTML,如果它是一个只包含元素和属性的XML文档,它可能会剥离所有内容。请尝试手动下载。在
    $mech->get('http://datawww2.wxc.com/kml/echo/MESH_Max_180min/');,键入
    $mech->get($urlOfDynamicLink);打印$mech->contents()
    您还可以一步保存文件:
    $mech->get('http://datawww2.wxc.com/kml/echo/MESH_Max_180min/MESH_Max_180min_20100707-130536.kmz“,”:content_file'=>“20100707_130536.kmz”)可能是这样,我不确定你以前有过什么,当我无法访问该页面时,很难看到该页面在做什么。如果URL有相对路径,那么可能。另外,如果您选择使用
    :content\u file=>
    使用第一个
    ->get()
    存储HTML文件,我认为
    $mech
    将是空的。它可能不起作用的原因有很多。不,我没有意识到,直到我回去看,它是在寻找那个特定的链接。它仍然不能正常工作,但这是一个开始。谢谢
    #!/usr/bin/perl -w
    
    use strict;
    use WWW::Mechanize;
    
       sub main{
       
          my $url    =  qq(http://www.kmzlinks.com);
          my $dest   =  qq($ENV{HOME}/Desktop/destfile.kmz);
          
          my $mech   =  WWW::Mechanize->new(autocheck => 1);
          
          # if needed, pass your credentials before this call
          $mech->get($url);
          die "Couldn't fetch page" unless $mech->success;
          
          # find all the links that have urls to kmz files
          my @links  =  $mech->find_all_links( url_regex => qr/(?:\.|%2E)kmz$/i );
          
          foreach my $link (@links){               # (loop example)
    
             # use absolute URL path of the link to download file to destination
             $mech->get($link->url_abs, ':content_file' => $dest);
         
             last;                                 # only need one (for testing)
          }     
       }
       
       main();