Debian Codegraph @

examples.html

raw
1 <html>
2 <head><title>HTML::TableExtract Examples</title></head>
3 <body>
4 <h2>HTML::TableExtract Examples</h2>
5 <p>
6 Each table is labeled in the first row with coordinates in terms of
7 <i>depth</i> and <i>count</i>, which both start at 0. Some of the tables
8 have <i>headers</i> in the second row; although in this example these header
9 cells are in fact &lt;th> tags, header cells can be either
10 &lt;th> or &lt;td>. The remaining cells in the table indicate <i>row</i>
11 and <i>column</i> information from that cell, along with the table
12 coordinates: <i>depth,count:row,column</i>. Rows and columns begin at 0 as
13 well, so the table label and headers, if present, will affect
14 these cell coordinates.
15 </p>
16 <p>In the illustrations of what is extracted from these tables, content in <em>italics</em> is notational in nature; it was not actually extracted from the tables. In particular, whenever <em>headers</em> are used for extraction, the order in which the headers were provided is noted by listing the headers, but the header row is not actually extracted from the target table.</p>
17 <p>It might be helpful to open a new browser window with this table visible so that the table can be easily examined when scrolling through the examples.
18 </p>
19 <table border=1 width="100%"><tr bgcolor="#33CCFF" valign="top"><td bgcolor="#33CCFF" colspan=2 valign="top">Table (0,0)</td></tr><tr bgcolor="#33CCFF" valign="top"><td bgcolor="#33CCFF" valign="top">0,0:1,0<table bgcolor="#66FF99" border=1 width="97%"><tr valign="top"><td colspan=3 valign="top">Table (1,0)</td></tr><tr valign="top"><th valign="top">East</th><th valign="top">Central</th><th valign="top">West</th></tr><tr valign="top"><td valign="top">1,0:2,0</td><td rowspan=3 valign="top">1,0:2,1</td><td valign="top">1,0:2,2</td></tr><tr valign="top"><td valign="top">1,0:3,0</td><td valign="top">1,0:3,2</td></tr><tr valign="top"><td valign="top">1,0:4,0</td><td valign="top">1,0:4,2</td></tr><tr valign="top"><td valign="top">1,0:5,0</td><td valign="top">1,0:5,1</td><td valign="top">1,0:5,2</td></tr></table></td><td bgcolor="#33CCFF" valign="top">0,0:1,1<table bgcolor="#66FF99" border=1 width="97%"><tr valign="top"><td colspan=3 valign="top">Table (1,1)</td></tr><tr valign="top"><th valign="top">Left</th><th valign="top">Middle</th><th valign="top">Right</th></tr><tr valign="top"><td valign="top">1,1:2,0</td><td valign="top">1,1:2,1</td><td valign="top">1,1:2,2</td></tr><tr valign="top"><td valign="top">1,1:3,0</td><td valign="top">1,1:3,1</td><td valign="top">1,1:3,2</td></tr><tr valign="top"><td valign="top">1,1:4,0</td><td valign="top">1,1:4,1</td><td valign="top">1,1:4,2</td></tr><tr valign="top"><td valign="top">1,1:5,0</td><td valign="top">1,1:5,1</td><td valign="top">1,1:5,2</td></tr></table></td></tr><tr bgcolor="#33CCFF" valign="top"><td bgcolor="#33CCFF" valign="top">0,0:2,0<table bgcolor="#66FF99" border=1 width="97%"><tr valign="top"><td colspan=2 valign="top">Table (1,2)</td></tr><tr valign="top"><th valign="top">Left</th><th valign="top">Right</th></tr><tr valign="top"><td valign="top">1,2:2,0<table bgcolor="#FFCC33" border=1 width="97%"><tr valign="top"><td colspan=2 valign="top">Table (2,0)</td></tr><tr valign="top"><th valign="top">Pacific</th><th valign="top">Atlantic</th></tr><tr valign="top"><td valign="top">2,0:2,0</td><td valign="top">2,0:2,1</td></tr><tr valign="top"><td valign="top">2,0:3,0</td><td valign="top">2,0:3,1</td></tr></table></td><td valign="top">1,2:2,1<table bgcolor="#FFCC33" border=1 width="97%"><tr valign="top"><td colspan=2 valign="top">Table (2,1)</td></tr><tr valign="top"><th valign="top">Lefty</th><th valign="top">Righty</th></tr><tr valign="top"><td valign="top">2,1:2,0</td><td valign="top">2,1:2,1</td></tr><tr valign="top"><td valign="top">2,1:3,0</td><td valign="top">2,1:3,1</td></tr></table></td></tr><tr valign="top"><td valign="top">1,2:3,0</td><td valign="top">1,2:3,1</td></tr><tr valign="top"><td valign="top">1,2:4,0</td><td valign="top">1,2:4,1</td></tr><tr valign="top"><td valign="top">1,2:5,0</td><td valign="top">1,2:5,1</td></tr></table></td><td bgcolor="#33CCFF" valign="top">0,0:2,1<table bgcolor="#66FF99" border=1 width="97%"><tr valign="top"><td colspan=3 valign="top">Table (1,3)</td></tr><tr valign="top"><th valign="top">Pacific</th><th valign="top">Plains</th><th valign="top">Atlantic</th></tr><tr valign="top"><td rowspan=2 valign="top">1,3:2,0</td><td valign="top">1,3:2,1</td><td valign="top">1,3:2,2</td></tr><tr valign="top"><td rowspan=2 valign="top">1,3:3,1</td><td valign="top">1,3:3,2</td></tr><tr valign="top"><td valign="top">1,3:4,0</td><td valign="top">1,3:4,2</td></tr><tr valign="top"><td colspan=2 valign="top">1,3:5,0</td><td valign="top">1,3:5,2</td></tr></table></td></tr></table>
20
21 <hr>
22 <strong>Example 1</strong>
23 <br>
24 <table bgcolor="#000000" border=0 cellpadding=0 cellspacing=0><tr><td><table bgcolor="#FFFFCC" border=0 cellpadding=5 cellspacing=1><tr><td><pre>$te = new HTML::TableExtract( headers =&gt; [qw(Right Left)] );
25 $te-&gt;parse($html_string);
26 </pre></td></tr></table></td></tr></table>
27
28 <br>
29 Result:
30 <br>
31 <table border=0 cellpadding=5><tr valign="top"><td valign="top"><table bgcolor="#DDDDDD" border=1><tr><td colspan=2><font size="-1">Extracted from table (1,1)</font></td></tr><tr><td colspan=2><font size="-1"><em>Order: Right, Left</em></font></td></tr><tr><td>1,1:2,2</td><td>1,1:2,0</td></tr><tr><td>1,1:3,2</td><td>1,1:3,0</td></tr><tr><td>1,1:4,2</td><td>1,1:4,0</td></tr><tr><td>1,1:5,2</td><td>1,1:5,0</td></tr></table></td><td valign="top"><table bgcolor="#DDDDDD" border=1><tr><td colspan=2><font size="-1">Extracted from table (2,1)</font></td></tr><tr><td colspan=2><font size="-1"><em>Order: Right, Left</em></font></td></tr><tr><td>2,1:2,1</td><td>2,1:2,0</td></tr><tr><td>2,1:3,1</td><td>2,1:3,0</td></tr></table></td><td valign="top"><table bgcolor="#DDDDDD" border=1><tr><td colspan=2><font size="-1">Extracted from table (1,2)</font></td></tr><tr><td colspan=2><font size="-1"><em>Order: Right, Left</em></font></td></tr><tr><td>1,2:2,1</td><td>1,2:2,0</td></tr><tr><td>1,2:3,1</td><td>1,2:3,0</td></tr><tr><td>1,2:4,1</td><td>1,2:4,0</td></tr><tr><td>1,2:5,1</td><td>1,2:5,0</td></tr></table></td></tr></table>
32
33 <br>
34
35 <br>
36 With headers, <i>depth</i> and <i>count</i> are irrelevant; all tables with columns matching those headers are extracted. Matches are accomplished as case-insensitive, non-anchored regular expressions. Columns are automatically rearranged in the same order as the headers were provided, so in this case we have reversed left and right. Rows above and including the rows where the headers were found are ignored; only the rows beneath the headers are extracted. Only the columns that line up with specific headers are retained.
37 <hr>
38 <strong>Example 2</strong>
39 <br>
40 <table bgcolor="#000000" border=0 cellpadding=0 cellspacing=0><tr><td><table bgcolor="#FFFFCC" border=0 cellpadding=5 cellspacing=1><tr><td><pre>$te = new HTML::TableExtract( headers =&gt; [qw(Lefty Righty)] );
41 $te-&gt;parse($html_string);
42 </pre></td></tr></table></td></tr></table>
43
44 <br>
45 Result:
46 <br>
47 <table border=0 cellpadding=5><tr valign="top"><td valign="top"><table bgcolor="#DDDDDD" border=1><tr><td colspan=2><font size="-1">Extracted from table (2,1)</font></td></tr><tr><td colspan=2><font size="-1"><em>Order: Lefty, Righty</em></font></td></tr><tr><td>2,1:2,0</td><td>2,1:2,1</td></tr><tr><td>2,1:3,0</td><td>2,1:3,1</td></tr></table></td></tr></table>
48
49 <br>
50
51 <br>
52 Using basic header extraction, tables can be reliably extracted from a document no matter how the HTML changes around them or deeply nested they are.
53 <hr>
54 <strong>Example 3</strong>
55 <br>
56 <table bgcolor="#000000" border=0 cellpadding=0 cellspacing=0><tr><td><table bgcolor="#FFFFCC" border=0 cellpadding=5 cellspacing=1><tr><td><pre>@tes = (
57 new HTML::TableExtract( headers =&gt; [qw(Pacific Plains Atlantic)] ),
58 new HTML::TableExtract( headers =&gt; [qw(Atlantic Pacific Plains)] ),
59 new HTML::TableExtract( headers =&gt; [qw(Atlantic Plains)] ),
60 new HTML::TableExtract( headers =&gt; [qw(Plains Pacific)] )
61 );
62 grep($_-&gt;parse($html_string), @tes);
63 </pre></td></tr></table></td></tr></table>
64
65 <br>
66 Result:
67 <br>
68 <table border=0 cellpadding=5><tr valign="top"><td valign="top"><table bgcolor="#DDDDDD" border=1><tr><td colspan=3><font size="-1">Extracted from table (1,3)</font></td></tr><tr><td colspan=3><font size="-1"><em>Order: Pacific, Plains, Atlantic</em></font></td></tr><tr><td>1,3:2,0</td><td>1,3:2,1</td><td>1,3:2,2</td></tr><tr><td></td><td>1,3:3,1</td><td>1,3:3,2</td></tr><tr><td>1,3:4,0</td><td></td><td>1,3:4,2</td></tr><tr><td>1,3:5,0</td><td></td><td>1,3:5,2</td></tr></table></td><td valign="top"><table bgcolor="#DDDDDD" border=1><tr><td colspan=3><font size="-1">Extracted from table (1,3)</font></td></tr><tr><td colspan=3><font size="-1"><em>Order: Atlantic, Pacific, Plains</em></font></td></tr><tr><td>1,3:2,2</td><td>1,3:2,0</td><td>1,3:2,1</td></tr><tr><td>1,3:3,2</td><td></td><td>1,3:3,1</td></tr><tr><td>1,3:4,2</td><td>1,3:4,0</td><td></td></tr><tr><td>1,3:5,2</td><td>1,3:5,0</td><td></td></tr></table></td><td valign="top"><table bgcolor="#DDDDDD" border=1><tr><td colspan=2><font size="-1">Extracted from table (1,3)</font></td></tr><tr><td colspan=2><font size="-1"><em>Order: Atlantic, Plains</em></font></td></tr><tr><td>1,3:2,2</td><td>1,3:2,1</td></tr><tr><td>1,3:3,2</td><td>1,3:3,1</td></tr><tr><td>1,3:4,2</td><td></td></tr><tr><td>1,3:5,2</td><td></td></tr></table></td><td valign="top"><table bgcolor="#DDDDDD" border=1><tr><td colspan=2><font size="-1">Extracted from table (1,3)</font></td></tr><tr><td colspan=2><font size="-1"><em>Order: Plains, Pacific</em></font></td></tr><tr><td>1,3:2,1</td><td>1,3:2,0</td></tr><tr><td>1,3:3,1</td><td></td></tr><tr><td></td><td>1,3:4,0</td></tr><tr><td></td><td>1,3:5,0</td></tr></table></td></tr></table>
69
70 <br>
71
72 <br>
73 The tables above represent different ways of extracting information from the same table using headers; notice how the column order is automatically adjusted to reflect the order in which the headers were provided. <i>Gridmapping</i> preserves the columns that you see in a browser. Tables are actually HTML tree structures, so when cell spans are involved, the "grid" is an illusion. <i>Gridmapping</i> superimposes a grid structure of 1x1 cells over the table, and reports columns intuitively. (note that the cell coordinates in this case represent these grid coordinates, rather than tree coordinates).
74 <hr>
75 <strong>Example 4</strong>
76 <br>
77 <table bgcolor="#000000" border=0 cellpadding=0 cellspacing=0><tr><td><table bgcolor="#FFFFCC" border=0 cellpadding=5 cellspacing=1><tr><td><pre>@tes = (
78 new HTML::TableExtract( depth =&gt; 1, count =&gt; 3 ),
79 new HTML::TableExtract( depth =&gt; 1, count =&gt; 3, gridmap =&gt; 0 )
80 );
81 grep($_-&gt;parse($html_string), @tes);
82 </pre></td></tr></table></td></tr></table>
83
84 <br>
85 Result:
86 <br>
87 <table border=0 cellpadding=5><tr valign="top"><td valign="top"><table bgcolor="#DDDDDD" border=1><tr><td colspan=3><font size="-1">Extracted from table (1,3)</font></td></tr><tr><td>Table (1,3)</td><td></td><td></td></tr><tr><td>Pacific</td><td>Plains</td><td>Atlantic</td></tr><tr><td>1,3:2,0</td><td>1,3:2,1</td><td>1,3:2,2</td></tr><tr><td></td><td>1,3:3,1</td><td>1,3:3,2</td></tr><tr><td>1,3:4,0</td><td></td><td>1,3:4,2</td></tr><tr><td>1,3:5,0</td><td></td><td>1,3:5,2</td></tr></table></td><td valign="top"><table bgcolor="#DDDDDD" border=1><tr><td colspan=3><font size="-1">Extracted from table (1,3)</font></td></tr><tr><td>Table (1,3)</td><td></td><td></td></tr><tr><td>Pacific</td><td>Plains</td><td>Atlantic</td></tr><tr><td>1,3:2,0</td><td>1,3:2,1</td><td>1,3:2,2</td></tr><tr><td>1,3:3,1</td><td>1,3:3,2</td><td></td></tr><tr><td>1,3:4,0</td><td>1,3:4,2</td><td></td></tr><tr><td>1,3:5,0</td><td>1,3:5,2</td><td></td></tr></table></td></tr></table>
88
89 <br>
90
91 <br>
92 Here we target the same table using <i>depth</i> and <i>count</i>. Taken together, <i>depth</i> and <i>count</i> uniquely specify at table in an HTML document, though it does introduce more context than using <i>headers</i>. Notice also that the entire table is retrieved, not just the columns beneath the headers. In the first example, <i>gridmapping</i> is enabled by default. In the second, it is explicity disabled in order to illustrate the tree ordering of cells.
93 <hr>
94 <strong>Example 5</strong>
95 <br>
96 <table bgcolor="#000000" border=0 cellpadding=0 cellspacing=0><tr><td><table bgcolor="#FFFFCC" border=0 cellpadding=5 cellspacing=1><tr><td><pre>$te = new HTML::TableExtract( depth =&gt; 2 );
97 $te-&gt;parse($html_string);
98 </pre></td></tr></table></td></tr></table>
99
100 <br>
101 Result:
102 <br>
103 <table border=0 cellpadding=5><tr valign="top"><td valign="top"><table bgcolor="#DDDDDD" border=1><tr><td colspan=2><font size="-1">Extracted from table (2,0)</font></td></tr><tr><td>Table (2,0)</td><td></td></tr><tr><td>Pacific</td><td>Atlantic</td></tr><tr><td>2,0:2,0</td><td>2,0:2,1</td></tr><tr><td>2,0:3,0</td><td>2,0:3,1</td></tr></table></td><td valign="top"><table bgcolor="#DDDDDD" border=1><tr><td colspan=2><font size="-1">Extracted from table (2,1)</font></td></tr><tr><td>Table (2,1)</td><td></td></tr><tr><td>Lefty</td><td>Righty</td></tr><tr><td>2,1:2,0</td><td>2,1:2,1</td></tr><tr><td>2,1:3,0</td><td>2,1:3,1</td></tr></table></td></tr></table>
104
105 <br>
106
107 <br>
108 When only a <i>depth</i> is specified, all tables at that depth are returned.
109 <hr>
110 <strong>Example 6</strong>
111 <br>
112 <table bgcolor="#000000" border=0 cellpadding=0 cellspacing=0><tr><td><table bgcolor="#FFFFCC" border=0 cellpadding=5 cellspacing=1><tr><td><pre>$te = new HTML::TableExtract( count =&gt; 1 );
113 $te-&gt;parse($html_string);
114 </pre></td></tr></table></td></tr></table>
115
116 <br>
117 Result:
118 <br>
119 <table border=0 cellpadding=5><tr valign="top"><td valign="top"><table bgcolor="#DDDDDD" border=1><tr><td colspan=3><font size="-1">Extracted from table (1,1)</font></td></tr><tr><td>Table (1,1)</td><td></td><td></td></tr><tr><td>Left</td><td>Middle</td><td>Right</td></tr><tr><td>1,1:2,0</td><td>1,1:2,1</td><td>1,1:2,2</td></tr><tr><td>1,1:3,0</td><td>1,1:3,1</td><td>1,1:3,2</td></tr><tr><td>1,1:4,0</td><td>1,1:4,1</td><td>1,1:4,2</td></tr><tr><td>1,1:5,0</td><td>1,1:5,1</td><td>1,1:5,2</td></tr></table></td><td valign="top"><table bgcolor="#DDDDDD" border=1><tr><td colspan=2><font size="-1">Extracted from table (2,1)</font></td></tr><tr><td>Table (2,1)</td><td></td></tr><tr><td>Lefty</td><td>Righty</td></tr><tr><td>2,1:2,0</td><td>2,1:2,1</td></tr><tr><td>2,1:3,0</td><td>2,1:3,1</td></tr></table></td></tr></table>
120
121 <br>
122
123 <br>
124 When only a <i>count</i> is specified, all tables at that <i>count</i> from each depth are returned. In this example, the second table within each <i>depth</i> is extracted (both <i>depth</i> and <i>count</i> begin with 0).
125 <hr>
126 <strong>Example 7</strong>
127 <br>
128 <table bgcolor="#000000" border=0 cellpadding=0 cellspacing=0><tr><td><table bgcolor="#FFFFCC" border=0 cellpadding=5 cellspacing=1><tr><td><pre>$te = new HTML::TableExtract( count =&gt; 1, headers =&gt; [qw(Left Middle Right)] );
129 $te-&gt;parse($html_string);
130 </pre></td></tr></table></td></tr></table>
131
132 <br>
133 Result:
134 <br>
135 <table border=0 cellpadding=5><tr valign="top"><td valign="top"><table bgcolor="#DDDDDD" border=1><tr><td colspan=3><font size="-1">Extracted from table (1,1)</font></td></tr><tr><td colspan=3><font size="-1"><em>Order: Left, Middle, Right</em></font></td></tr><tr><td>1,1:2,0</td><td>1,1:2,1</td><td>1,1:2,2</td></tr><tr><td>1,1:3,0</td><td>1,1:3,1</td><td>1,1:3,2</td></tr><tr><td>1,1:4,0</td><td>1,1:4,1</td><td>1,1:4,2</td></tr><tr><td>1,1:5,0</td><td>1,1:5,1</td><td>1,1:5,2</td></tr></table></td></tr></table>
136
137 <br>
138
139 <br>
140 When constraints are specified together, they each have a veto power on whether to extract the table. In this case, the same two tables in the prior example matched on this <i>count</i>, but the <i>header</i> constraint discarded the one without the proper headers.
141 <hr>
142 </body>
143 </html>