Solved

convert HTML table to .csv

Posted on 2002-04-10
7
1,437 Views
Last Modified: 2008-02-01
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
Comment
Question by:frenomulax
7 Comments
 
LVL 1

Accepted Solution

by:
smisk earned 100 total points
ID: 6931962
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
 
LVL 1

Expert Comment

by:bluprint
ID: 6931984

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

That should do it...
0
 
LVL 1

Expert Comment

by:bluprint
ID: 6932053
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
Independent Software Vendors: 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!

 
LVL 16

Expert Comment

by:maneshr
ID: 6932213
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
 

Author Comment

by:frenomulax
ID: 6932301
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
 
LVL 84

Expert Comment

by:ozo
ID: 6932454
But beware of tricky cases like:
<IMG SRC = "foo.gif" ALT = "A > B">
<!-- <A comment> -->
0
 

Author Comment

by:frenomulax
ID: 6932760
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question