问题
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