1

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.

Ebikeneser
  • 2,582
  • 13
  • 57
  • 111

2 Answers2

11

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
4

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.

bvr
  • 9,687
  • 22
  • 28