http://www.perlmonks.org?node_id=1212267

unklejunky has asked for the wisdom of the Perl Monks concerning the following question:

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.