Parse html using Perl

冷暖自知 提交于 2019-12-11 06:19:12

问题


I have the following html-

<a href="http://address.com">John</a>: I really <b>love</b> <b>soccer</b>;

I want to parse it into a csv where I would have

name = John

comment = I really love soccer.

key words = love, soccer

in the console app, any help is much appreciated.


回答1:


Here is an example how to do parsing with HTML::TreeBuilder:

use HTML::TreeBuilder;

my $html = HTML::TreeBuilder->new_from_content(<<END_HTML);
<a href="http://address.com">John</a>: I really <b>love</b> <b>soccer</b>;
END_HTML

my $name     = $html->find('a')->as_text;               # "John"
my @keywords = map { $_->as_text } $html->find('b');    # "love", "soccer"
my $comment  = $html->as_text;                          # "John: I really love soccer; "

Cleaning up $comment is left as an exercise.




回答2:


There are plenty of HTML parsers on CPAN, my preferred one is HTML::TreeBuilder::XPath

Text::CSV will help you generate a CSV from the extracted data.



来源:https://stackoverflow.com/questions/7319756/parse-html-using-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!