Reputation: 47
I have written a script to login to a web page and print the response. Now I want to find a string in the HTML response, but I don't know how.
My current code:
use strict;
use warnings;
use LWP::UserAgent;
my $clientIP = "129.168.1.50:80";
my $clientURL = "http://" . $clientIP . "/conf";
## User Agent (UA)
my $ua=LWP::UserAgent->new;
$ua->timeout(10);
$ua->credentials($clientIP, 'Secure Area', 'user', 'pa$$word');
my $page = $ua->get($clientURL);
my $body = $page->content();
print $body;
Current print output from $body
:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>configuration</title>
</head>
<body>
<h1>Client</h1>
<p>
Version
2.16.4.9</p>
<a href="settings">Settings</a>
<br>
<a href="updateskin">Update skin</a>
<br>
<a href="updatesettings">Update settings</a>
<br>
<p>Software Solutions</p>
</body>
</html>
How can I find the version string and number in the response and write it to a variable?
The goal of the script is to get the version number and write it to a file.
Upvotes: 1
Views: 825
Reputation: 126742
I suggest that you use HTML::TreeBuilder
which will process the HTML for you and allow you to navigate the resulting structure
The code would look like this
use strict;
use warnings 'all';
use HTML::TreeBuilder;
my $client_ip = '129.168.1.50:80';
my $client_url = "http://$client_ip/conf";
my $tree = HTML::TreeBuilder->new_from_url($client_url);
my $version;
for my $p ( $tree->look_down(_tag => 'p') ) {
my $text = $p->as_trimmed_text;
if ( $text =~ / version /ix ) {
$version = $text;
last;
}
}
print $version, "\n";
Upvotes: 2
Reputation: 47
Thanks for your help. The regular expression is the simplest solution.
# Remove all wordwraps
while($body=~s/[\n\r\l]//){}
my $ver='unknown'; # Default version
if($body=~/version\s*([0-9\.]+)/i){$ver=$1;}
print "\n" . $ver . "\n";
Upvotes: -1
Reputation: 132865
Here's a Mojo example. There are a few differences between LWP::UserAgent and Mojo::UserAgent that make it worth it for me. First, the get
returns a transaction that knows about the request and the response. That may not be important here, but I find I often want it. Next, I can immediately get a DOM object and call find
on it with CSS selectors, such as h1 > p
which means "the p after h1". From that I get a collection which I can call map
on. In this case, I tell map
to call the text
method on each item from the collection of found thingys:
use Mojo::UserAgent;
my $url = "http://$user:$password\@$clientIP/conf";
my $ua = Mojo::UserAgent->new;
my $tx = $ua->get($url);
my( $version ) = $tx->res->dom->find( 'h1 > p' )->map( 'text' );
The last thing I like is that Mojolicious is self-contained. I don't have to install multiple things and risk something outside of Mojo breaking part of the installation process.
Upvotes: 1