Autotrader's website has a form called 'searchVehicle'. In that form are a number of HTML <option> values for the given form input fields `qr/^(radius|make|model|price-to)`. These <option> values are as you'd expect, i.e. a drop-down list. How do I pull the string values of those `<option>`'s?
So far I have the following Perl code:
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use WWW::Mechanize;
use Data::Dumper;
use JSON;
binmode STDOUT, ':encoding(UTF-8)';
binmode STDIN, ':encoding(UTF-8)';
my $url = 'https://www.autotrader.co.uk/';
my $mech = WWW::Mechanize -> new( autocheck => 1 );
$mech -> agent_alias( 'Linux Mozilla');
if ($mech -> status( $mech -> get($url)) == 200)
{
$mech -> form_name('searchVehicles');
my @inputs = $mech -> find_all_inputs(
name_regex => qr/^(radius|make|mode
+l|price-to)$/,
type => 'option',);
print Dumper \@inputs;
};
My result is like:
$VAR1 = [
bless( {
'class' => 'c-form__select',
'current' => 0,
'id' => 'radius',
'menu' => [
{
'name' => 'Distance (national)',
'value' => '',
'seen' => 1
}
],
'type' => 'option',
'aria-label' => 'Choose a distance from your postco
+de',
'name' => 'radius',
'idx' => 1
}, 'HTML::Form::ListInput' ),
bless( {
}, 'HTML::Form::ListInput' ),
bless( {
}, 'HTML::Form::ListInput' ),
bless( {
}, 'HTML::Form::ListInput' )
];
Note: I have truncated all but the first since you get the idea with the first.
The above code returns an array of hashes. I can access all the values that are listed in the Dumper output if I loop over the `@inputs` array and print only the current loop's hash key I want for the corresponding value, but I am after the values of the `<option>` entries. For example, if you inspect the source of the website `searchVehicles` form has an input field for Distance. The drop-down gives you choices between 1 to 200 miles.
How do I obtain those values? This is so I can use these values to present and prompt the user of the script with valid options for their search.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.