I have created a little sample perl script to add to liz's comment above. The sample script demonstrates the use of the WWW::Mechanize module. It fetches the url first, fills in the question, submits the form, gets the answer, and reformats the answer (optionally) to plain text wrapping at column 60.

use strict; use WWW::Mechanize; my $url = "http://www.ai.mit.edu/projects/infolab/"; my $question = 'What is AI'; my $robot = new WWW::Mechanize; $robot->get($url); $robot->form_number('1'); $robot->set_fields('query' => $question); # ask a question $robot->click(); # Get the reply to my question my $html = $robot->content(); # Extract the answer my ($text) = $html =~ /(<H1>START(?:.|\n)*<HR>)/mg; # Reformat the text $text =~ s/<[^>]*>//g; # Strip HTML tags $text =~ s/(?<!\n)\n(?!\n)/ /mg; # Combine lines $text =~ y/ //s; # Squash multiple spaces
my $len; $text =~ s/((\S+)(?=\n|\s)|\n)/ # Reformat plain text if ($1 eq "\n") { # wrapping at col 60 $len = 0; $1 } elsif ($len + length($1) > 60) { $len = length($1) + 1; "\n$1" } else { $len += length($1) + 1; $1 } /mge;
# after some playing around, I came up with the # following regex that does wrapping at column # 60 perfectly. I love perl. ;-) $text =~ s/(.{50,60}(?<=\s\b))/$1\n/mg; print "$text\n";
And the formatted answer to my question is -
START's reply ===> What is AI Artificial Intelligence is the study of the computations that make it possible to perceive, reason and act. From the perspective of this definition, artificial intelligence differs from most of psychology because of the greater emphasis on computation, and artificial intelligence differs from most of computer science because of the emphasis on perception, reasoning and action. The central goal of the Artificial Intelligence Laboratory is to develop a computational theory of intelligence extending from the manifestation of behavior to operations at the neural level. Current work focuses especially on understanding the role of vision, language, and motor computation in general intelligence.

In reply to Re: How to "fake" web browser from Perl by Roger
in thread How to "fake" web browser from Perl by Anonymous Monk

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.