【问题标题】:Get image src links from urls using perl and store in DB使用 perl 从 url 获取图像 src 链接并存储在数据库中
【发布时间】:2018-04-13 15:52:29
【问题描述】:

我正在尝试使用以下 Perl 代码提取图像 src 链接。不要得到我犯错误的地方。 1.打开一个文件并读取其中的URL

我的文本文件是这样的

https://zzzzzz.com/
https://yyyyyyy.com/
https://xxxxxxxxxx.com/
https://stackoverflow.com/
https://www.google.com/
https://www.yahoo.com/
  1. foreach提取img src的文本文件中的URL
  2. 将检索到的数据打印到另一个文件中
  3. 再次使用新文件句柄打开文件并将其读入数组
  4. 取消引用数组时显示错误ARRAY(0x2e14a48) ARRAY(0x3125528) ARRAY(0x312e170)

Perl 代码是

#!/usr/bin/perl
print "Content-type: text/html\n\n";
use strict;
use warnings;
use DBI;
use LWP::Simple;
use HTML::LinkExtor;

my $filename = "/path/to/file";

open FILE, '<', $filename or print "cant open file: $!";
my @data = <FILE>;
close(FILE);

my $image = "/path/to/file";

open FILES, '>', $image or print "cant write to file: $!";

foreach my $urls (@data) {
   my $url = get("$urls");

   my $linkextor = HTML::LinkExtor->new( \&links );

   $linkextor->parse($url);

   my $key;

   sub links {
      ( my $tag, my %links ) = @_;
      if ( $tag eq "img" ) {
         foreach my $key ( keys %links ) {
            if ( $key eq "src" ) {
               foreach my $da ( @{$links{$key}} ) {
                  if ( $da =~ /^[a-zA-Z]/ ) {
                     print FILES "$da;\n";
                  } #if
               } #foreach
            }    #if
         }    #foreach
      }    #if
   }    #sub

   print FILES "\n";

}    #foreach
close(FILES);

到此为止,没有问题我得到了所有的src链接,比如

https://zzzzzz.com/;https://yyyyyyy.com/;https://xxxxxxxxxx.com/;

https://zzzzzz.com/;https://yyyyyyy.com/;https://xxxxxxxxxx.com/;

https://zzzzzz.com/;https://yyyyyyy.com/;https://xxxxxxxxxx.com/;

https://zzzzzz.com/;https://yyyyyyy.com/;https://xxxxxxxxxx.com/;

这是我在文本文件中输出的格式,我只需要在图像列中按$image1, $image2, $image3 的顺序插入所有这些网址

my $platform = "mysql";
my $database = "xxx";
my $host     = "xxxxx";
my $port     = "xxxx";
my $user     = "xxxxx";
my $pw       = "xxxxxxxxx";

my $dbh = DBI->connect( "DBI:$platform:$database:$host:$port", $user, $pw );

open FILED, '<', $image or die "cannot open file: $!";
my @img = <FILED>;
close(FILED);

foreach my $lin (@img) {
   chomp $lin;
   my @in     = split ';', $lin;
   my $image1 = $in[0];
   my $image2 = $in[1];
   my $image3 = $in[2];

   print "$image1 $image2 $image3 \n";

   $sth->execute( $li, $val, $parsed, $htmls, $image1, $image2, $image3 );

}

exit;

我认为我在 foreach 循环中犯了错误,对吗?提前致谢。

【问题讨论】:

  • ...为什么在 foreach 循环中嵌入了子定义?

标签: perl


【解决方案1】:

你的问题很可能在这里:

foreach my $da ( $links{$key} ) {

因为看起来您假设 $links{$key} 是一个数组,但当它不可能时 - 它只能 是一个数组引用。如果您打印它,这将遇到您描述的问题 - 它会输出ARRAY(0xDEADBEEF) 类型格式,因为这就是数组引用字符串化的方式。

所以您可能会发现将其更改为:

foreach my $da ( @{$links{$key}} ) {

会成功的。

但我也建议

  • 在 foreach 循环中嵌入 sub 是不好的风格。
  • 使用带有词法文件句柄的 3 个参数打开 - 例如open my $input, '&lt;', 'file.name' or die $!
  • 使用while 循环对其进行迭代,而不是将其读入一个您不重复使用的数组中。
  • 您两次声明my $key - 第一个实例未被使用,并且具有误导性。
  • 您将输出写入$imageFILES,然后打开同一个文件并再次读回。不过,您似乎不需要中间文件,那么为什么不首先将它存储在 @img 数组中呢?

【讨论】:

  • 同样值得注意的是@data应该是chomped,虽然我认为LWP::Simple会忽略尾随空格。
【解决方案2】:

你的问题就在这里。

my @in     = split ';', $lin;
my $image1 = [0];
my $image2 = [1];
my $image3 = [2];

您正在将匿名数组分配给您的变量。上面的行应该是这样的。

my $image1 = $in[0];
my $image2 = $in[1];
my $image3 = $in[2];

【讨论】:

    猜你喜欢
    • 2011-06-12
    • 2020-05-20
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 2016-04-20
    • 2011-12-15
    • 2021-10-13
    相关资源
    最近更新 更多