使用 WWW::Mechanize::Firefox 从列表项中提取文本
Pulling text from list items using WWW::Mechanize::Firefox
鉴于以下 HTML:
<div class="chosen-drop">
<ul class="chosen-results">
<li>Stuff 1</li>
<li>Stuff 2</li>
<li>Stuff 3</li>
</ul>
</div>
如何使用 WWW::Mechanize::Firefox xpath
function?
从列表项中提取文本
这似乎应该可行,它基本上是从文档中提取的,但它是空的:
my @text = $mech->xpath('//div[@class="chosen-drop"]/ul/li/text()');
我一定是 xpath 遗漏了什么。
使用这些文件:
mech_xpath.pl:
#!perl -w
use strict;
use WWW::Mechanize::Firefox;
use Data::Dump qw/dump/;
my $mech = WWW::Mechanize::Firefox->new();
$mech->get_local('local.html');
my @text = $mech->xpath('//div[@class="chosen-drop"]/ul/li/text()');
warn dump \@text;
<>;
local.html:
<div class="chosen-drop">
<ul class="chosen-results">
<li>Stuff 1</li>
<li>Stuff 2</li>
<li>Stuff 3</li>
</ul>
</div>
给出此输出:
[
bless({
# tied MozRepl::RemoteObject::TiedHash
}, "MozRepl::RemoteObject::Instance"),
bless({
# tied MozRepl::RemoteObject::TiedHash
}, "MozRepl::RemoteObject::Instance"),
bless({
# tied MozRepl::RemoteObject::TiedHash
}, "MozRepl::RemoteObject::Instance"),
]
看起来一切正常。您如何检查 @text
的内容?
鉴于以下 HTML:
<div class="chosen-drop">
<ul class="chosen-results">
<li>Stuff 1</li>
<li>Stuff 2</li>
<li>Stuff 3</li>
</ul>
</div>
如何使用 WWW::Mechanize::Firefox xpath
function?
这似乎应该可行,它基本上是从文档中提取的,但它是空的:
my @text = $mech->xpath('//div[@class="chosen-drop"]/ul/li/text()');
我一定是 xpath 遗漏了什么。
使用这些文件:
mech_xpath.pl:
#!perl -w
use strict;
use WWW::Mechanize::Firefox;
use Data::Dump qw/dump/;
my $mech = WWW::Mechanize::Firefox->new();
$mech->get_local('local.html');
my @text = $mech->xpath('//div[@class="chosen-drop"]/ul/li/text()');
warn dump \@text;
<>;
local.html:
<div class="chosen-drop">
<ul class="chosen-results">
<li>Stuff 1</li>
<li>Stuff 2</li>
<li>Stuff 3</li>
</ul>
</div>
给出此输出:
[
bless({
# tied MozRepl::RemoteObject::TiedHash
}, "MozRepl::RemoteObject::Instance"),
bless({
# tied MozRepl::RemoteObject::TiedHash
}, "MozRepl::RemoteObject::Instance"),
bless({
# tied MozRepl::RemoteObject::TiedHash
}, "MozRepl::RemoteObject::Instance"),
]
看起来一切正常。您如何检查 @text
的内容?