Hi poj
It is working fine for the HTML table I gave with NAME ,ID and RESULT as status. But if the webpage contains the following content then it fails :
It gives the following error
Can't call method "tag" without a package or object reference at /usr/bin/perl/lib/HTML/ElementTable.pm line 367.
The HTML is as follows :
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U
+S">
<head>
<title>DV</title>
<link rel="stylesheet" type="text/css" href="/css/tree.css" />
<script src="/js/tree.js" type="text/javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1
+" />
</head>
<body bgcolor="#FFFFFF">
<br\><TABLE><TR><TD><img align="left" src="/images/ver/logo100.gif" al
+t="[LOGO]"
/><TD><h1>Reg</h1><h3>MC:Test </h3></TD></TR></TABLE>
<br /><p /><a href="ab.pl"><b>[overview]</b></a> <a
href="ab.pl?path=/usr/proj">[RUN]</a> <a
href="s.pl?path=/usr/proj">[DB]</a> <a
href="q.pl?path=/usr/proj">[Report]</a><p /><br />
<FONT SIZE=-1><I>(Click on the hyperlinks in the table headers to see
+information on methods [if available])</I></FONT><BR>
<TABLE BORDER=1><TR BGCOLOR="#D0D0D0"><TH ALIGN=LEFT NOWRAP>List</TH><
+TH
ALIGN=LEFT>St1</TH><TH ALIGN=LEFT>rc</TH><TH ALIGN=LEFT>st2</TH><TH
ALIGN=LEFT>st3</TH><TH ALIGN=LEFT>st4</TH><TH ALIGN=LEFT><A
HREF="m.pl?path=/usr/proj;task=layverDoc;command=exec"st5</A></TH><TH
+ALIGN=LEFT><A
HREF="m.pl?path=/usr/proj;task=lw;command=exec">layverShadow</A></TH><
+TH
ALIGN=LEFT>lvs</TH><TH ALIGN=LEFT>st6</TH><TH ALIGN=LEFT>st7</TH><TH
ALIGN=LEFT>pi</TH><TH ALIGN=LEFT><A
HREF="m.pl?path=/usr/proj;task=prefix;command=exec">pix</A></TH></TR>
<TR CLASS='node' ID='ck' VALIGN=top><TD ALIGN=LEFT NOWRAP><A CLASS=bla
+nk HREF="" ONCLICK="toggleRow(this.parentNode.parentNode.parentNode,
+'ck'); return false;"> <B>clock_x2</B></A><B></B></TD><TD BGCOLO
+R="#D0D0D0"></TD><TD BGCOLOR="#D0D0D0"></TD><TD BGCOLOR="#D0D0D0"></T
+D><TD BGCOLOR="#D0D0D0"></TD><TD BGCOLOR="#D0D0D0"></TD><TD BGCOLOR="
+#D0D0D0"></TD><TD BGCOLOR="#D0D0D0"></TD><TD BGCOLOR="#D0D0D0"></TD><
+TD BGCOLOR="#D0D0D0"></TD><TD BGCOLOR="#D0D0D0"></TD><TD BGCOLOR="#D0
+D0D0"></TD><TD BGCOLOR="#D0D0D0"></TD></TR>
<TR CLASS='node' ID='ck@ck' VALIGN=top><TD ALIGN=LEFT NOWRAP> &
+nbsp; <B>clock_x2</B><B></B></TD><TD><FONT COLOR="green">O
+K</FONT></TD><TD>-</TD><TD><FONT COLOR="green">OK</FONT></TD><TD><FON
+T COLOR="green">OK</FONT></TD><TD><FONT COLOR="green">OK</FONT></TD><
+TD><FONT COLOR="green">OK</FONT></TD><TD><FONT COLOR="green">OK</FONT
+></TD><TD>-</TD><TD><FONT COLOR="green">OK</FONT></TD><TD><FONT COLOR
+="green">OK</FONT></TD><TD><FONT COLOR="green">OK</FONT></TD><TD><FON
+T COLOR="green">OK</FONT></TD></TR>
</TABLE>
<BR><hr>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100%>
<TR>
<TD WIDTH=42%>
<FONT SIZE=-2>© Copyright(c) 1996<BR>
Company </FONT></TD>
<TD WIDTH=42%>
<FONT SIZE=-2>Last modified 2013 <BR>
by <A HREF="mailto:res@next.com">res</A>
</TD>
<TD>
<IMG BORDER=0 SRC="/images/ver/ver_dev_team.jpg" ALIGN=RIGHT HSP
+ACE=0 VSPACE=0 ALT="[ver]">
</TD>
</TR>
</TABLE>
<p /><br />
</body>
</html>
And my code which is not working for the above html is below. But it is working successfully for the previous html I mentioned.
#!/usr/bin/perl
use strict;
use warnings;
use Spreadsheet::WriteExcel;
use HTML::TableExtract qw(tree);
require LWP::UserAgent;
use HTML::Element;
my $ua = new LWP::UserAgent();
my $request = HTTP::Request->new(GET=>"http://autoreport.nextgen.com";
+);
my $response = [];
$response = $ua->request($request);
$response = $response->content();
my $data = [];
@$data = split(/\n/, $response);
# Create a new Excel file
my $filename = "/home/user/result.xls";
my $workbook = Spreadsheet::WriteExcel->new($filename);
# Add a worksheet
my $worksheet = $workbook->add_worksheet('exec2');
# Define the format and add it to the worksheet
my $format = $workbook->add_format(
center_across => 1,
bold => 1,
size => 10,
border => 4,
color => 'black',
bg_color => 'cyan',
border_color => 'black',
align => 'vcenter',
);
my $htm;
foreach(@$data)
{
$htm .= $_ ;
}
print "htm $htm \n";
my $te = HTML::TableExtract->new(); # headers => ["ID", "NAME" ,"DOB"]
+ );
$te->parse($htm); # ($response);
my $nrow= 0;
my %fmt = ();
for my $ts ($te->tables) {
print "ts $ts \n";
for my $row ($ts->rows) {
my $ncol = 0;
for my $el (@$row) {
my $tag = $el->find_by_tag_name('font');
my $color = (defined $tag) ? $tag->attr('color'): 'white';
if(! exists $fmt{$color}) {
$fmt{$color} = $workbook->add_format(bg_color=>$co
+lor);
}
$worksheet->write($nrow,$ncol++,$el->as_text,$fmt{$col
+or}) if
defined $el;
}
$nrow++
}
$nrow++
}
|