Posts with the tag 'html'

Perl 的 html 解析模块

HTML::TreeBuilder
这个解析模块使用了强大的 HTML::Element 模块。解析时,HTML::TreeBuilder模块把整个 html 文档转换成了 perl 的数据结构,可以进行任意的操作。
使用时先创建一个 HTML::TreeBuilder 对象。
use Data::Dumper qw(Dumper);
$Data::Dumper::Indent = 1;
use HTML::TreeBuilder;
my $tree = new HTML::TreeBuilder;
直接向 HTML::TreeBuilder 传递文件好像会把中文转换成 unicode 的字符,所以一般传递文件句柄。而且传递的字符串或者句柄一般要先确保是 utf8 字符串才行,不然会有一个warning:
Parsing of undecoded UTF-8 will give garbage when decoding entities at /home/ywb/temp/t.pl line 16, line 5.
解析文件和解析字符串的区别仅仅是前者是使用 parse_file 函数,而后者使用parse 函数。下面以解析文件句柄的例子:
binmode DATA, “utf8″;
$tree->parse_file(\*DATA);
print Dumper($tree), “\n”;
__DATA__

x
y

1
2

要提取出表格中的内容可以这样:
foreach my $row ( $tree->find_by_tag_name(“tr”) ) {
foreach my $cell [...]

Add comment 十二月 29th, 2009

1 of 11

 

2010年三月
« 二    
1234567
891011121314
15161718192021
22232425262728
293031  

分类目录

标签

书签 优化 参数 命令 啪嗒砰 域名 备份 字体 安装 导入 导出 扩展 换行 文字 正则 爬虫 解决办法 路径 道具 错误 镜像 19楼 Apache awk AWstats CentOS CPAN EditPlus Firefox GD GeoIP Google IBM Linux mysql OUTLOOK Patapon perl photoshop PHP profile rsync sed Shell Thunderbird

最近文章

最近评论

文章索引模板

功能

SEO Powered by Platinum SEO from Techblissonline