HTML table to CSV


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting HTML table to CSV
# 1  
Old 11-15-2007
HTML table to CSV

Hi !!
I have HTML Tables through which i want to generate graphs, but for creating graphs i need the file in CSV format so can anyone can please help me in how can i convert my HTML table file to CSV format.
Thanks in Advance
# 2  
Old 11-15-2007
try this perl script (tested with input html file containing only one html table):
Code:
#!/usr/bin/perl
# csv_from_table.pl
use strict;
my $html_file = shift;
my $csv_file  = shift;
open (F_CSV, ">", $csv_file)    or  die "Failed to write to file $csv_file : $!";
open (F_HTML, "<", $html_file)  or  die "Failed to read file $html_file : $!";
while (<F_HTML>) {
# read html file line by line
    while (m#<TD>\s*(\d+)\s*</TD>\s*(</TR>)*#gi) {
    # keep searching for numbers within TD tags, with an optional /TR tag at the end
        if (! $2) {
        # this TD is not the last TD in the TR
            print F_CSV "$1,";
              # so write comma after this number
        }
        else {
        # this is the last TD in the TR
            print F_CSV "$1\n";
              # so write newline after this number
        }
    }
}
close (F_HTML);
close (F_CSV);

run this script as:
Code:
perl csv_from_table.pl table_data.html newfile.csv

html file that i used as input (table_data.html):
Code:
<HTML>
<HEAD>
<TITLE>Table with numeric data</TITLE>
</HEAD>
<BODY>
<TABLE border="1">
  <TR> <TD>5</TD> <TD>4</TD>
 <TD>23</TD> </TR> <TR> <TD>10</TD> <TD>3</TD> <TD>24</TD> </TR>
  <TR> <TD>6</TD> <TD>12</TD> <TD>28</TD> </TR>
  <TR> <TD>17</TD> <TD>20</TD> <TD>32</TD> </TR>
</TABLE>
</BODY>
</HTML>

# 3  
Old 11-15-2007
Hi.

If you have command lynx (a text-mode browser) installed, it does a good job of removing markup tags:
Code:
% cat s1
#!/usr/bin/env sh

# @(#) s1       Demonstrate lynx -dump to eliminate html tags.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash lynx sed tr

echo

FILE=${1-data1.html}

echo " Input data:"
cat $FILE

echo
echo " Final results:"

lynx -dump $FILE |
tee t1 |
sed -e 's/^ *//' |
tr -s ' ' ','

echo
echo " Intermediate results from lynx:"
cat t1

exit 0

Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0
Lynx Version 2.8.5rel.1 (04 Feb 2004)
GNU sed version 4.1.2
tr (coreutils) 5.2.1

 Input data:
<HTML>
<HEAD>
<TITLE>Table with numeric data</TITLE>
</HEAD>
<BODY>
<TABLE border="1">
  <TR> <TD>5</TD> <TD>4</TD>
 <TD>23</TD> </TR> <TR> <TD>10</TD> <TD>3</TD> <TD>24</TD> </TR>
  <TR> <TD>6</TD> <TD>12</TD> <TD>28</TD> </TR>
  <TR> <TD>17</TD> <TD>20</TD> <TD>32</TD> </TR>
</TABLE>
</BODY>
</HTML>

 Final results:

5,4,23
10,3,24
6,12,28
17,20,32

 Intermediate results from lynx:

   5  4  23
   10 3  24
   6  12 28
   17 20 32

The lynx -dump output needs only a bit of a massage to get it into CSV format. See man lynx for details ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Export HTML table

HI , I have a HTML tables as below. It has 2 tables ,I want to extract the second table . Please help me in doing it. <html> <body> <b><br>Running Date: </b>11-JAN-2019 03:07</br> <h2> Schema mapping and info </h2> <BR><TABLE width="100%" class="x1h" cellpadding="1"... (3 Replies)
Discussion started by: deepti01
3 Replies

2. Linux

Parsing - export html table data as .csv file?

Hi all, Is there any out there have a brilliant idea on how to export html table data as .csv or write to txt file with separated comma and also get the filename of link from every table and put one line per rows each table. Please see the attached html and PNG of what it looks like. ... (7 Replies)
Discussion started by: lxdorney
7 Replies

3. UNIX for Dummies Questions & Answers

Extract table from an HTML file

I want to extract a table from an HTML file. the table starts with <table class="tableinfo" and ends with next closing table tag </table> how can I do this with awk/sed... ---------- Post updated at 04:34 PM ---------- Previous update was at 04:28 PM ---------- also I want to... (4 Replies)
Discussion started by: koutroul
4 Replies

4. Shell Programming and Scripting

Creating html table from data in file

Hi. I need to create html table from file which contains data. No awk please :) In example, ->cat file num1 num2 num3 23 3 5 2 3 4 (between numbers and words single TAB). after running mycode i need to get (heading is the first line): <table>... (2 Replies)
Discussion started by: Manu1234567
2 Replies

5. Web Development

Help on filtering the table in HTML

1. how to get the filter option on table so that user can enter the fields which ever they want to print only according to the need ? 2.how to print the full fledge table if there is no value in the rows of the table but it should print the whole rows and column in proper tabular form? (2 Replies)
Discussion started by: sidhi
2 Replies

6. Shell Programming and Scripting

Itinerate throught HTML table

HI all, <html> <body> <div> <table id="orderList"> <thead> <tr> <th>order number</th> <th>order type</th> <th>product type</th> <th>status</th> <th>status date</th> </tr> </thead> <tbody> <tr class="odd"> ... (10 Replies)
Discussion started by: valigula
10 Replies

7. Shell Programming and Scripting

Get HTML table

Hi all, I have a html that contains several tables in it. Need to extract the data from one of them named "orderList". Is it any easy way without using loops. Thanks (4 Replies)
Discussion started by: valigula
4 Replies

8. Shell Programming and Scripting

help with a bash script to create a html table

Hi guys as the title says i need a little help i have partisally written a bash script to create a table in html so if i use ./test 3,3 i get the following output for the third arguement in the script i wish to include content that will be replace the A characters in the... (2 Replies)
Discussion started by: dunryc
2 Replies

9. Shell Programming and Scripting

PHP: Sorting HTML table

Hi All, I have an html table which looks like this: <table align="center" border="1"> <CAPTION><EM>Heading for Table</EM></CAPTION> <tr><td><b>1</b></TD><TD><b>2</b></TD><TD><b>3</b></TD><TD><b>4</b></TD><TD><b>TOTAL</b></TD><TD><b>DATE</b></td></tr> <tr><td>88088283</TD> <TD>87613101</TD>... (1 Reply)
Discussion started by: pondlife
1 Replies

10. Shell Programming and Scripting

Export a HTML table to Xcel

Hello All, I have a perl script that prints a HMTL table. I want to convert this data into a report and this want to export this information into Excel. How can I do this? Regards, garric (3 Replies)
Discussion started by: garric
3 Replies
Login or Register to Ask a Question