【发布时间】:2018-04-10 03:16:22
【问题描述】:
Autotrader 的网站有一个名为“searchVehicle”的表单。在该表单中是给定表单输入字段qr/^(radius|make|model|price-to) 的许多HTML 值。这些值如您所料,即下拉列表。如何提取那些<option> 的字符串值?
到目前为止,我有以下 Perl 代码:
#!/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|model|price-to)$/,
type => 'option',);
print Dumper \@inputs;
};
我的结果是这样的:
$VAR1 = [
bless( {
'idx' => 1,
'type' => 'option',
'current' => 0,
'name' => 'radius',
'menu' => [
{
'name' => 'Select the distance',
'seen' => 1,
'value' => ''
}
],
'id' => 'radius',
'aria-label' => 'Choose a radius',
'class' => 'c-form__select'
}, 'HTML::Form::ListInput' ),
bless( {
}, 'HTML::Form::ListInput' ),
bless( {
}, 'HTML::Form::ListInput' ),
bless( {
}, 'HTML::Form::ListInput' )
];
注意:我已经截断了除第一个之外的所有内容,因为您了解第一个的想法。
- 如何返回
'HTML::Form::ListInput'值?
【问题讨论】:
-
能否提供实际页面的网址?
-
@zdim - 我已经编辑了代码。
标签: perl