Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How do I pull HTML form option values?

by marto (Cardinal)
on Apr 04, 2018 at 08:09 UTC ( [id://1212304]=note: print w/replies, xml ) Need Help??


in reply to How do I pull HTML form option values?

Here's an example of how to do this using Mojo::DOM to get you started, below I simply copy the HTML you provided and use it as a variable, a better method is discussed after the output:

use strict; use warnings; use feature 'say'; use Mojo::DOM; my $html = '<select class="c-form__select" id="radius" name="radius" a +ria-label="Choose a distance from your postcode"><option value="">Dis +tance (national)</option><option value="1">Within 1 mile</option><opt +ion value="5">Within 5 miles</option><option value="10">Within 10 mil +es</option><option value="15">Within 15 miles</option><option value=" +20">Within 20 miles</option><option value="25">Within 25 miles</optio +n><option value="30">Within 30 miles</option><option value="35">Withi +n 35 miles</option><option value="40">Within 40 miles</option><option + value="45">Within 45 miles</option><option value="50">Within 50 mile +s</option><option value="55">Within 55 miles</option><option value="6 +0">Within 60 miles</option><option value="70">Within 70 miles</option +><option value="80">Within 80 miles</option><option value="90">Within + 90 miles</option><option value="100">Within 100 miles</option><optio +n value="200">Within 200 miles</option></select>'; my $dom = Mojo::DOM->new( $html ); # find each select foreach my $select ( $dom->find('select')->each ){ say "Found select named $select->{name} with the values/text:"; # process each option foreach my $opt ( $select->find('option')->each ){ say $opt->{value}; say $opt->text; } }

Produces:

Found select named radius with the values/text: Distance (national) 1 Within 1 mile 5 Within 5 miles 10 Within 10 miles 15 Within 15 miles 20 Within 20 miles 25 Within 25 miles 30 Within 30 miles 35 Within 35 miles 40 Within 40 miles 45 Within 45 miles 50 Within 50 miles 55 Within 55 miles 60 Within 60 miles 70 Within 70 miles 80 Within 80 miles 90 Within 90 miles 100 Within 100 miles 200 Within 200 miles

If $html contained additional <select> elements each will be processed e.g.

.... Within 200 miles Found select named derp with the values/text: foo bar

You could combine the whole thing with the page get using Mojo::UserAgent like I do in this example, which gets a page, parses the dom for a selector and prints a value.

A word of warning, I couldn't see a terms of use on the site you linked to, none was visible where I could easily find it when I browsed this morning, using a phone. Many sites list automatic scraping as a violation of their terms of use.

Update: slight rewording for clarity.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1212304]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-24 02:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found