I am adding one more method which uses HTML::TreeBuilder::XPath to select what you want using an XPath expression. Mainly for sentimental reasons because TreeBuilder helped me out in many a cases, but also because XPath expressions are useful to know and use.

A word of warning: HTML::TreeBuilder seems to ignore unknown-to-it html tags by default, e.g. HTML5 tags like section. A quick but dirty fix is to instruct it to lax a bit, see Issues using HTML::TreeBuilder::XPath and the HTML5 <section> tag

Here is example code tested with your html string:

#!/usr/bin/env perl use strict; use warnings; use HTML::TreeBuilder::XPath; 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 $TB = HTML::TreeBuilder::XPath->new( # if you have html5 with tags unknown to 'HTML::TreeBuilder' then +uncomment following # ignore_unknown => 0, ); $TB->parse($html) || die 'TB->parse()'; #my $xpath = '//form[@name="searchVehicles"]//select[@name="radius"]/o +ption[@value!=""]/@value'; my $xpath = '//select[@name="radius"]//option[@value!=""]/@value'; my @values = $TB->findvalues($xpath); print "found ".scalar(@values)." option values:\n"; foreach my $avalue (@values){ print "value: '".$avalue."'\n"; }

p.s. Personally, I would use a package which can cope better with latest HTML standards. XPath for me is appealing, say like a regex is. However, if what you are building is going to be a long term, expanding project you need to maintain - easily - your XPaths whenever site's web design changes. Maybe then Mojo::DOM (which I have not tried) is the way to go.


In reply to Re: How do I pull HTML form option values? by bliako
in thread How do I pull HTML form option values? by unklejunky

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.