【发布时间】:2010-12-11 16:45:15
【问题描述】:
可能重复:
Can you provide an example of parsing HTML with your favorite parser?
How can I extract content from HTML files using Perl?
我正在尝试在 Perl 中使用正则表达式来解析具有以下结构的表。第一行如下:
<tr class="Highlight"><td>Time Played</a></td><td></td><td>Artist</td><td width="1%"></td><td>Title</td><td>Label</td></tr>
这里我想取出“Time Played”、“Artist”、“Title”和“Label”,并将它们打印到输出文件中。
任何帮助将不胜感激!
好的,抱歉...我尝试了很多正则表达式,例如:
$lines =~ / (<td>) /
OR
$lines =~ / <td>(.*)< /
OR
$lines =~ / >(.*)< /
我当前的程序如下所示:
#!perl -w
open INPUT_FILE, "<", "FIRST_LINE_OF_OUTPUT.txt" or die $!;
open OUTPUT_FILE, ">>", "PLAYLIST_TABLE.txt" or die $!;
my $lines = join '', <INPUT_FILE>;
print "Hello 2\n";
if ($lines =~ / (\S.*\S) /) {
print "this is 1: \n";
print $1;
if ($lines =~ / <td>(.*)< / ) {
print "this is the 2nd 1: \n";
print $1;
print "the word was: $1.\n";
$Time = $1;
print $Time;
print OUTPUT_FILE $Time;
} else {
print "2ND IF FAILED\n";
}
} else {
print "THIS FAILED\n";
}
close(INPUT_FILE);
close(OUTPUT_FILE);
【问题讨论】:
-
这不是那个问题的真正重复。
-
@Kinopiko:足够接近。想要提取 td 标签和 li 标签之间的部分有什么区别?
-
顺便说一句,您似乎对您的任务感到困惑:您尝试解析的文本在标签内。你想要的字符串是标记的,可以这么说。