?
Solved

convert HTML table to .csv

Posted on 2002-04-10
7
Medium Priority
?
1,545 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Accepted Solution

by:
smisk earned 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans
Suggested Courses

777 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