I am trying to extract image src links using the following Perl code. Don't get where I am making mistake. 1. open a file and read URLs in it
My text file looks like this
https://zzzzzz.com/
https://yyyyyyy.com/
https://xxxxxxxxxx.com/
https://stackoverflow.com/
https://www.google.com/
https://www.yahoo.com/
foreachURL in text file extracting img src- print the retrieved data into another file
- again open the file using new file handle and read it into an array
- while dereferencing array it shows error
ARRAY(0x2e14a48) ARRAY(0x3125528) ARRAY(0x312e170).
Perl code is
#!/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);
Until this, there is no problem I got all the src links like
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/;
This is the format I have output in the text file, all I need is to insert all these urls by order as $image1, $image2, $image3 in image column
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;
I thought that I am making mistakes in foreach loop, am I right. Thanks in advance.