[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1662
  • Last Modified:

convert HTML table to .csv

I need to write a script to convert the contents of a HTML table to a .csv file, which will then be imported into an Excel spreadsheet. So far, thanks to some help from the good folks at this site, I have a way to extract the table from the rest of the content on the web page. What I need now is a way to convert this to comma seperated values.

My plan has been to use regular expressions to convert all of the </TD> tags to commas, all of the </TR> tags to new lines, and strip aaway all other tags, leaving a .csv file. I started writing regular expressions to remove 1 tag at a time, sort of like this:

# turn the </TR> tag into a newline...
($in_file =~ s/\<\/TR\>/\n/gi);
# turn the </TD> tag into a comma...
($in_file =~ s/\<\/TD\>/,/gi);
# strip out the <TR> tag...
($in_file =~ s/\<TR\>//gi);
# strip out the <TD> tags...
($in_file =~ s/\<TD[\s]*?(.*)\>//gi);
# strip out the <P> tags...
($in_file =~ s/\<P\>//gi);

but to me this just seems awkward and wrong. So, is there a grandaddy of all regular expressions to parse out all HTML tags, leaving only the data? It seems if I can use the first two above, then all I have to do is pull all of the other tags.

I'm looking for suggestions as to how I can best accomplish this. On qualification, I expect there is a module to help parse out HTML, but I can only do this using modules which come with a standard perl5.005_03 build, as I don't have sysad rights here at work to add modules into perl.

Thanks
0
frenomulax
Asked:
frenomulax
1 Solution
 
smiskCommented:
This will strip all HTML tags.  I suggest running the table ones (TD, TR above) first...

#! /usr/local/bin/perl

while(<>) {
        s/<[^>]+>//g;
        print;
}
0
 
bluprintCommented:

# strip out the <TR>, <TD>, <P> tags...
($in_file =~ s/(?:\<TR\>) || (?:\<TD[\s]*?(.*)\>) || (?:\<P\>)//gi);

That should do it...
0
 
bluprintCommented:
To make:

# strip out the <TR>, <TD>, <P> tags...
($in_file =~ s/(?:\<TR\>) || (?:\<TD[\s]*?(.*)\>) || (?:\<P\>)//gi);

A little cleaner:

# strip out the <TR>, <TD>, <P> tags...
($in_file =~ s/(?:\<(?:TD||TR||P)[\s]*?([^\/>]*)\>)//gi;);


This also corrects an error. Where you are looking for <TD[\s]*?(.*)\>) you should be looking for <TD[\s]*?([^\/>])\>)

Otherwise, you will remove everthing from:

<TD> Here to here including both beggining and ending tags </TD>
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
maneshrCommented:
frenomulax,

".. I  have a way to extract the table from the rest of the content on the web page...."

Can you please post the script that does this extraction?
Also, can you post some sample documents on which this script works?

Please provide any other relevant details. This will help you get a more accurate answer, faster.
0
 
frenomulaxAuthor Commented:
Although the other answers on this page are more thorough and robust, smisk's answer is working just fine for this purpose, so you get the points this time.

Thanks
0
 
ozoCommented:
But beware of tricky cases like:
<IMG SRC = "foo.gif" ALT = "A > B">
<!-- <A comment> -->
0
 
frenomulaxAuthor Commented:
But beware of tricky cases like:
<IMG SRC = "foo.gif" ALT = "A > B">
<!-- <A comment> -->

well, that's just plain evil  :-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now