简体   繁体   English

PERL LWP :: Simple getstore在哪里保存图像?

[英]Where does PERL LWP::Simple getstore save the image?

I am trying to use perl getstore to get a list of image from URL after reading a text file containing the file names, I created the code and able to run it successfully but I do not know where is the file saved, i checked the disk size it and shows that every time i run the code the hard disk free space decrease, so i assume there are file saved but I can't find it. 在读取包含文件名的文本文件后,我尝试使用perl getstore从URL获取图像列表,我创建了代码并能够成功运行它,但是我不知道文件保存在哪里,我检查了磁盘调整大小并显示每次运行代码时,硬盘的可用空间都会减少,因此我假设已保存了文件,但找不到。 So where does perl getstore save file and what is the correct way to save image from a link ? 那么,perl getstore在哪里保存文件,从链接保存图像的正确方法是什么?

use strict;
use warnings;
use LWP::UserAgent;
use LWP::Simple;

my $url = "https://labs.jamesooi.com/images/";
my $ua = LWP::UserAgent->new;
$ua->agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36");

my $file = 'image-list.txt';
open (DATA, $file) or die "Could not open $file: $!";

while(<DATA>){
  my $link = "$url" . "$_";
  my $filename = "$_";
  print $link;
  print $filename;

  my $req = HTTP::Request->new(GET => $link);
  my $res = $ua->request($req);
  if($res->is_success){
    my $rc = getstore($link, $filename);
    if(is_success($rc)){
      print "Success\n";
    }else{
      print "Error\n";
    }
  } else {
    print $res->status_line, "\n";
  }
}

According to the documentation , getstore(url, file) takes the URL as the first argument and the second argument is the file name where the result is stored. 根据文档getstore(url, file)将URL作为第一个参数,第二个参数是存储结果的文件名。 If the file name is a relative path (it doesn't begin with a slash / ) it will be relative to the current working directory. 如果文件名是相对路径(不是以斜杠/开头),它将相对于当前工作目录。

But you read the name from a file and then treat the full line, including the newline character, as the file name. 但是,您从文件中读取了名称,然后将包括换行符在内的全行作为文件名。 That's probably not what you want, so you should use chomp to remove the newline. 那可能不是您想要的,所以您应该使用chomp删除换行符。

Apart from that: 除此之外:

  • You are doing first a GET request using LWP::UserAgent to retrieve the file but ignore the response and instead call getstore to retrieve and store the same resource if the first GET was successful. 您首先使用LWP::UserAgent进行GET请求以检索文件,但忽略响应,而是在第一个GET成功的情况下调用getstore来检索和存储相同的资源。 It would be simpler to either just save the result from the first GET or just skip it and use only getstore . 仅保存第一个GET的结果,或者仅跳过它并仅使用getstore会更简单。

  • You are using DATA as a file handle. 您正在使用DATA作为文件句柄。 While this is not wrong, DATA is already an implicit file handle which points to the program file after the __DATA__ marker, so I recommend to use a different file handle. 尽管这没有错,但是DATA已经是一个隐式文件句柄,该句柄指向__DATA__标记之后的程序文件,因此,我建议使用其他文件句柄。

When using a simplified version of the code the file gets successfully stored: 使用简化版本的代码时,文件将成功存储:

use strict;
use warnings;    

use LWP::Simple;

my $url  = "https://labs.jamesooi.com/images/";
my $file = 'image-list.txt';

open (my $fh, '<', $file) or die "Could not open $file: $!";

while ( <$fh> ) {

    chomp;  # remove the newline from the end of the line

    my $link     = $url . $_;
    my $filename = $_;

    my $rc = getstore($link, $filename);

    if (is_success($rc)) {
        print "Success\n";
    }
    else {
        print "Error\n";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM