我是 Perl 的新手,我正在尝试读取<div class="one">
HTML 文件之间的特定内容。
HTML 文件:
<div class="one">
<div id="two">Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
</div>
<pre>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
</pre>
</div>
Perl 代码:
my $file = "content.html";
if (-e $file) {
open(IN, $file);
while (<IN>) {
chomp($line = $_);
#print "$line\n";
}
}
@contents = <IN>;
#check to if content in html file is in the right location,
#if content is in correct location (div class="one")
#print content in div two and three if exist
for (my $i = 0 ; $i <= $#contents ; $i++) {
if (!$contents[$i] =~ m/^\s*<div/ && $contents[$i] =~ m/class\s*=\s*"one"/) {
print "content in wrong location";
}
else {
if ($contents[$i] =~ m/^\s*<div/) {
print "$_";
}
else ($contents[$i] =~ m/^\s*<pre/) {
print "$_";
}
}
}