Solved

convert HTML table to .csv

Posted on 2002-04-10
7
1,361 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
Comment Utility
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
Comment Utility

# 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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 16

Expert Comment

by:maneshr
Comment Utility
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
Comment Utility
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
Comment Utility
But beware of tricky cases like:
<IMG SRC = "foo.gif" ALT = "A > B">
<!-- <A comment> -->
0
 

Author Comment

by:frenomulax
Comment Utility
But beware of tricky cases like:
<IMG SRC = "foo.gif" ALT = "A > B">
<!-- <A comment> -->

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now