代码之家  ›  专栏  ›  技术社区  ›  shinjuo

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

  •  2
  • shinjuo  · 技术社区  · 14 年前

    希望这是我最后一个问题。我正在使用$mech->点击链接下载文件。出于某种原因,尽管保存的文件只是我第一次打开的页面,而不是我想要跟随的链接。这是我从链接下载文件的正确方式吗?我不想使用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");
    
    3 回复  |  直到 14 年前
        1
  •  3
  •   Community CDub    4 年前

    1. get ,以确保访问的是有效的HTML页
    2. 确保您要访问的链接是第三个名为“MESH”的链接(区分大小写?)
    3. 打印第二页的内容 得到
    4. 检查文件是否已成功创建

    附加


    例子

    #!/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();
    
        2
  •  1
  •   Zaid    14 年前

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

        3
  •  -1
  •   mcandre    14 年前

    if unless .