在 Perl 中用 url 解析特定 html 表的好习惯
给定一个带有如下表数据的 html...
<tr class=nbg1><td><A HREF=api.dll?pgm=cdq32&p1=oavmsd&p2=fg3m9s5d&p3=&cmd=w1d27d0id9654&hl=antibio&lstid=026>Nadifloxacin</A></td><td>Aknetherapeutikum Antibiotikum (Gyrasehemmer)</td><td>WST</td><td></td></tr>
<tr class=nbg2><td><A HREF=api.dll?pgm=cdq32&p1=oavmsd&p2=fg3m9s5d&p3=&cmd=w1d27d0id9728&hl=antibio&lstid=026>Ertapenem</A></td><td>Antibiotikum</td><td>WST</td><td></td></tr>
<tr class=nbg1><td><A HREF=api.dll?pgm=cdq32&p1=oavmsd&p2=fg3m9s5d&p3=&cmd=w1d27d0id9761&hl=antibio&lstid=026>Panipenem</A></td><td>Beta-Lactam-Antibiotikum</td><td>WST</td><td></td></tr>
<tr class=nbg2><td><A HREF=api.dll?pgm=cdq32&p1=oavmsd&p2=fg3m9s5d&p3=&cmd=w1d27d0id10302&hl=antibio&lstid=026>Prulifloxacin</A></td><td>Antibiotikum (Gyrasehemmer)</td><td>WST</td><td></td></tr>
</table></td>
<td width=15></td><td valign=top nowrap class=NBG1>
<TABLE width="200" border="0" cellspacing="0" cellpadding="2">
<TR><TD CLASS="NBG2">
</TD></TR></TABLE><BR>
我需要解析 url 和 url 描述,其中提取的 url 将用于进一步解析子页面。完成此操作的好习惯是什么,尤其是获取 url。
当前代码:
my $te = HTML::TableExtract->new( depth => 3, count => 0 );
$te->parse($mainpage);
my $ts = "";
my $row = "";
foreach $ts ($te->tables) {
foreach $row ($ts->rows) {
print @$row[0] . "\n";
}
}
Given a html with table data like the following...
<tr class=nbg1><td><A HREF=api.dll?pgm=cdq32&p1=oavmsd&p2=fg3m9s5d&p3=&cmd=w1d27d0id9654&hl=antibio&lstid=026>Nadifloxacin</A></td><td>Aknetherapeutikum Antibiotikum (Gyrasehemmer)</td><td>WST</td><td></td></tr>
<tr class=nbg2><td><A HREF=api.dll?pgm=cdq32&p1=oavmsd&p2=fg3m9s5d&p3=&cmd=w1d27d0id9728&hl=antibio&lstid=026>Ertapenem</A></td><td>Antibiotikum</td><td>WST</td><td></td></tr>
<tr class=nbg1><td><A HREF=api.dll?pgm=cdq32&p1=oavmsd&p2=fg3m9s5d&p3=&cmd=w1d27d0id9761&hl=antibio&lstid=026>Panipenem</A></td><td>Beta-Lactam-Antibiotikum</td><td>WST</td><td></td></tr>
<tr class=nbg2><td><A HREF=api.dll?pgm=cdq32&p1=oavmsd&p2=fg3m9s5d&p3=&cmd=w1d27d0id10302&hl=antibio&lstid=026>Prulifloxacin</A></td><td>Antibiotikum (Gyrasehemmer)</td><td>WST</td><td></td></tr>
</table></td>
<td width=15></td><td valign=top nowrap class=NBG1>
<TABLE width="200" border="0" cellspacing="0" cellpadding="2">
<TR><TD CLASS="NBG2">
</TD></TR></TABLE><BR>
I need to parse the url and the url description, where the extracted url will be used for further parsing the subpage. What would be a good practice to accomplish this, especially getting the url.
current code:
my $te = HTML::TableExtract->new( depth => 3, count => 0 );
$te->parse($mainpage);
my $ts = "";
my $row = "";
foreach $ts ($te->tables) {
foreach $row ($ts->rows) {
print @$row[0] . "\n";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想从该表中的每个 a' 元素中提取 href 属性,则无需使用 TableExtract,只需使用
HTML::Query
未经测试,但您明白了...
if you want to extract only the href attribute from each a' element in that table, no need to use TableExtract, just use
HTML::Query
no tested, but you get the idea...
HTML::TableExtract 可以帮助您准确处理表格。
HTML::TableExtract can help you exactly with dealing with tables.