解析html文件的最简单方法是什么,以便以后可以将其输入数据库?

时间:2012-07-06 16:16:12

标签: perl parsing

我有一个需要解析的~2 MB html文件,其中包含大约500行和大约70列。我需要以一种我以后可以将其输入SQL Server数据库的方式清理它。我过去使用Perl解析过文件,但从来没有html文件,我想知道在进行简单匹配和格式化之前是否有任何模块应该检查。

一点点更新:

<td class="tableHeaderDarkCenter">CUSIP/ISIN</td>

<td class="tableHeaderDarkCenter">Stock Ticker</td>

<td class="tableHeaderDarkCenter">MLCC Code</td>

<td class="tableHeaderDarkCenter">Bond Ticker</td>

<td class="tableHeaderDarkCenter">Issuer Name</td>

<td class="tableHeaderDarkCenter">Convertible Price(USD)</td>

<td class="tableHeaderDarkCenter">Par Amount</td>

<td class="tableHeaderDarkCenter">Coupon</td>

<td class="tableHeaderDarkCenter">Maturity/Mandatory Conversion Date</td>

<td class="tableHeaderDarkCenter">Outstanding Amt ($MM)</td>

<td class="tableHeaderDarkCenter">Bonds/Shrs Outstanding</td>

<td class="tableHeaderDarkCenter">Market Value($MM)</td>

<td class="tableHeaderDarkCenter">Index Weight(%)</td>

<td class="tableHeaderDarkCenter">YTM(%)</td>

<td class="tableHeaderDarkCenter">YTP(%)</td>

<td class="tableHeaderDarkCenter">Greater of YTM/YTP(%)</td>

<td class="tableHeaderDarkCenter">Duration</td>

<td class="tableHeaderDarkCenter">Currency</td>

<td class="tableHeaderDarkCenter">Country</td>

<td class="tableHeaderDarkCenter">Series</td>

<td class="tableHeaderDarkCenter">Accrued Interest</td>

<td class="tableHeaderDarkCenter">Current Yield(%)</td>

<td class="tableHeaderDarkCenter">Yield Advantage(%)</td>

<td class="tableHeaderDarkCenter">Moody Rating</td>

<td class="tableHeaderDarkCenter">S&P Rating</td>

<td class="tableHeaderDarkCenter">Avg. Rating</td>

<td class="tableHeaderDarkCenter">Internal Rating</td>

<td class="tableHeaderDarkCenter">Collateral Type</td>

<td class="tableHeaderDarkCenter">Status</td>

<td class="tableHeaderDarkCenter">Security Type</td>

<td class="tableHeaderDarkCenter">Announce Date</td>

<td class="tableHeaderDarkCenter">Issue Date</td>

<td class="tableHeaderDarkCenter">At-Issue Yield</td>

<td class="tableHeaderDarkCenter">At-Issue Prem</td>

<td class="tableHeaderDarkCenter">Delta</td>

<td class="tableHeaderDarkCenter">Gamma</td>

<td class="tableHeaderDarkCenter">RHO</td>

<td class="tableHeaderDarkCenter">Theoretical Value</td>

<td class="tableHeaderDarkCenter">Theoretical Discount (%)</td>

<td class="tableHeaderDarkCenter">Cheap (%)</td>

<td class="tableHeaderDarkCenter">Conversion Ratio</td>

<td class="tableHeaderDarkCenter">Parity Cash Adjustment</td>

<td class="tableHeaderDarkCenter">Payback</td>

<td class="tableHeaderDarkCenter">Implied Volatility(%)</td>

<td class="tableHeaderDarkCenter">Implied Spread</td>

<td class="tableHeaderDarkCenter">Parity Delta</td>

<td class="tableHeaderDarkCenter">Conversion Premium(%)</td>

<td class="tableHeaderDarkCenter">Investment Value Premium(%)</td>

<td class="tableHeaderDarkCenter">Investment Value(Bond floor)</td>

<td class="tableHeaderDarkCenter">Price to Par</td>

<td class="tableHeaderDarkCenter">Next Put Date</td>

<td class="tableHeaderDarkCenter">Yrs to Put</td>

<td class="tableHeaderDarkCenter">Yrs to Mat/Mand Conv Date</td>

<td class="tableHeaderDarkCenter">Yrs to Mat/Yrs to Put</td>

<td class="tableHeaderDarkCenter">Years to Call</td>

<td class="tableHeaderDarkCenter">1-Day Total Return(%)</td>

<td class="tableHeaderDarkCenter">1-WK Total Return(%)</td>

<td class="tableHeaderDarkCenter">MTD Total Return(%)</td>

<td class="tableHeaderDarkCenter">QTD Total Return(%)</td>

<td class="tableHeaderDarkCenter">YTD Total Return(%)</td>

<td class="tableHeaderDarkCenter">Index Sector</td>

<td class="tableHeaderDarkCenter">Industry</td>

<td class="tableHeaderDarkCenter">CS Sector L1</td>

<td class="tableHeaderDarkCenter">CS Sector L2</td>

<td class="tableHeaderDarkCenter">CS Sector L3</td>

<td class="tableHeaderDarkCenter">CS Sector L4</td>

<td class="tableHeaderDarkCenter">CS Sector L5</td>

<td class="tableHeaderDarkCenter">ML Sector L1</td>

<td class="tableHeaderDarkCenter">ML Sector L2</td>

<td class="tableHeaderDarkCenter">ML Sector L3</td>

<td class="tableHeaderDarkCenter">ML Sector L4</td>

<td class="tableHeaderDarkCenter">GIC Sector</td>

<td class="tableHeaderDarkCenter">GIC Industry Group</td>

<td class="tableHeaderDarkCenter">GIC Industry</td>

<td class="tableHeaderDarkCenter">GIC Sub Industry</td>

<td class="tableHeaderDarkCenter">Bloomberg Sector</td>

<td class="tableHeaderDarkCenter">Stock Price(USD)</td>

<td class="tableHeaderDarkCenter">Stock Yield</td>

<td class="tableHeaderDarkCenter">1-Day Equity Total Return(%)</td>

<td class="tableHeaderDarkCenter">1-WK Equity Total Return(%)</td>

<td class="tableHeaderDarkCenter">MTD Equity Total Return(%)</td>

<td class="tableHeaderDarkCenter">QTD Equity Total Return(%)</td>

<td class="tableHeaderDarkCenter">YTD Equity Total Return(%)</td>

<td class="tableHeaderDarkCenter">Eq Mkt Value($MM)</td>

这是文件中发生的情况,从文件结尾到文件末尾是映射到列名的相应值。显然很多。我给了HTML::TableExtract一个镜头,但我不确定它是否适合这种情况。

2 个答案:

答案 0 :(得分:5)

这里的朋友是HTML::TableExtract。它编写得很好(在五篇评论中共收到五颗星中的五颗),并允许您以非常方便的方式指定要提取的数据。


<强>更新

为了演示将HTML::TableExtract应用于某些HTML数据是多么容易,这里有一些代码可以打印文件中第一个表的每一行。

如果文件中有多个表,则必须使用模块提供的几种方法之一来选择其中一个。

每行的数据以Perl数组的形式返回,使用DBI可以很容易地存储在数据库中。

use strict;
use warnings;

use HTML::TableExtract;

my $te = HTML::TableExtract->new;
$te->parse_file('data_snippet.txt');
my $table = $te->first_table_found;

for my $row ($table->rows) {
  print "@$row\n";
}

答案 1 :(得分:1)

使用现成的HTML解析器,例如HTML::ParserHTML::TreeBuilder等。然后简单地遍历DOM中的表。