Solved

convert HTML table to .csv

Posted on 2002-04-10
7
1,395 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl program to obtain a machine's memory usage 6 23
Perl string replace for refred url 9 62
Writing a parser for java language 4 79
File Find regex problem 4 62
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

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