Search code examples
perllwp

Getting data from an updating website using perl


I've been trying to make a perl program that tells me the water level of a river from an updating website (https://www.vizugy.hu/?mapModule=OpGrafikon&AllomasVOA=73F7E310-985C-11D4-BB62-00508BA24287&mapData=Idosor), but my program can't access the website and I'm completely stuck and I'm a beginner.

#!/usr/bin/perl -w

$url = "https://www.vizugy.hu/?mapModule=OpGrafikon&AllomasVOA=73F7E310-985C-11D4-BB62-00508BA24287&mapData=Idosor";

use LWP::Simple;

$site = get($url) or die "The webpage won't load";

if($site =~ /<strong>(\d+)<\/strong>/ig){
    $waterLevel= $1;
    }else{
    die "Can't find the water level (Vízállás (cm))";
}

if($site =~ /<strong>(\d+.\d+.\d+. \d+.:\d+)<\/strong>/){
    $date = $1;
    }else{
    die "Can't find date (Időpont)";
}


print("The water level in Komarom is $waterLevel cm (Date: $date)\n");

I do this for a class and I have to use LWP. The website is in Hungarian and so were the variables, but I tried to translate as much as I could.


Solution

  • Your code works as expected from my Linux command line. But I see exactly the same behaviour as you when using this online IDE.

    The problem with LWP::Simple is that it's hard to debug what's going wrong. So I've replaced the top part of your code so it uses LWP::UserAgent instead.

    #!/usr/bin/perl
    
    # Always use these
    use strict;
    use warnings;
    
    my $url = "https://www.vizugy.hu/?mapModule=OpGrafikon&AllomasVOA=73F7E310-985C-11D4-BB62-00508BA24287&mapData=Idosor";
    
    use LWP::UserAgent;
    
    print "Make a UA\n";
    my $ua = LWP::UserAgent->new;
    
    print "Request\n";
    my $resp = $ua->get($url) or die "The webpage won't load";
    
    print "Response\n";
    print $resp->code, ': ', $resp->message, "\n";
    
    my $site = $resp->content;
    
    my ($waterLevel, $date);
    
    if ($site =~ /<strong>(\d+)<\/strong>/ig) {
        $waterLevel= $1;
    }else{
        die "Can't find the water level (Vízállás (cm))";
    }
    
    if ($site =~ /<strong>(\d+.\d+.\d+. \d+.:\d+)<\/strong>/) {
        $date = $1;
    }else{
        die "Can't find date (Időpont)";
    }
    
    print("The water level in Komarom is $waterLevel cm (Date: $date)\n");
    

    The response I see is:

    Make a UA
    Request
    Response
    500: Can't connect to www.vizugy.com:433 (Temporary failure in name resolution)
    Can't find the water level (Vízállás (cm)) at main.pl line 21
    

    So it looks like your online IDE isn't set up correctly to make HTTP requests. You could contact the owners (the email address is on the front page of their site) or you could just report the problem to your lecturer.