I'm scraping a HTML table of Cruise Ships and their locations, etc., using HTML::TableExtract and I wanted to use a "less brittle" approach. I know the desired column names in the table, but trying to extract based on that is yielding zero results. I can get the table by defining the border but I didn't really want that.

# get the data from the web. Typically this is: # http://www.sailwx.info/shiptrack/cruiseships.phtml # Either pass this in as --url <page_url> when invoking or just set it +. $cols = 'Ship,last reported (UTC),position,Callsign'; $url = "http://www.sailwx.info/shiptrack/cruiseships.phtml"; my $input; my $out_fn = 'C:\\Program Files\\cron\\Cruise Ships\\ship_data.csv'; open(my $out_fh, '>', $out_fn) or die("Unable to create output file \"$out_fn\": $!\n"); my $m = WWW::Mechanize->new(); $m->get($url); $input = $m->content; my $te; if ( defined ($cols)) { my @headers = split(/,/, $cols); te = HTML::TableExtract->new( attribs => { border => 1 } ); } else { $te = new HTML::TableExtract( depth => $depth, count=>$count); } $te->parse($input);
Neither of the following two extraction approaches seemed to work on the HTML. I had to find the table by border (as you can see in above code). This results in something more fragile than I would like.

I thought at least one of these two lines would work but they don't:

$te = new HTML::TableExtract ( headers => [qw(Ship position)] ); $te = new HTML::TableExtract(headers=>\@headers);

I even tried this line...

$te = HTML::TableExtract->new( headers => \@headers);
... but it doesn't work either. Well, I didn't really think it was a problem with spaces. I don't know what the problem is.


In reply to Extracted HTML table by mcoblentz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.