This is different enough that I thought a new response was warranted rather than just an update to the previous response. I'm not sure if the "sentiment values" actually correspond to the graph on the website, but these results seem reasonable and seem to "jive" with the "sentiment meter" readings.
Here is some LWP code for you.
Figuring out (a) what URL to send the POST back to can be frustrating as well as (b) figuring out the parameters that it wants to have - with LWP you have to decide this from looking at the page source code that is sent to you (that may include Javascript, and this one does). Usually you don't have to run the Javascript yourself, just reply with what that script would do.
Update:#!/usr/bin/perl -w use strict; use LWP::UserAgent; $|=1; my $DEBUG =0; my $url = "http://sentimentanalyzer.appspot.com/"; my $ua = LWP::UserAgent->new or die "New UserAgent Failed"; # get the main page ... this works ... # my $response = $ua->get($url) or die "Problem getting $url\n"; $response->is_success or die "Failed to GET '$url': ", $response->status_line; my $html_page = $response->content( ); print $html_page if $DEBUG; # The trouble starts with figuring out the correct stuff to # POST back to the website... # You have to know what URL to post back to - (not necessarily # exactly where it came from) and also what to send in the POST! # This site does not appear to use cookies # The URL to POST back to is slightly different than the # main URL - here is not "dynamic" and probably this works # even without retrieving the main site - When SSL and cookies # get involved, it can be more complicated. foreach my $content ("A dog not like a cat.", "This is bogus!", "I really love my grandmother.", ) { $response = $ua->post( 'http://sentimentanalyzer.appspot.com/api/classify', [ 'content' => "$content", 'value' => "Submit", 'lang' => 'en', ], ); $response->is_success or die "Error: ", $response->status_line; $html_page = $response->content(); print $html_page if $DEBUG; #This is "Very Ugly", but appears to work my ($score) = $html_page =~ /\{\"score\":(.*)\s*\}/; print "$score $content\n"; } __END__ 0.003720114371614687 A dog not like a cat. 0.81868251185499441 This is bogus! 0.90770799032922367 I really love my grandmother.
Firefox has moved some of this stuff around. To view the page source sent to the browser: Tools|Web Developer|Page Source. There are other tools like HTTPfox and others that I don't know how to use well yet, but other Monks will!
In reply to Re: Passing Values to a webpage and retrieving an answer
by Marshall
in thread Passing Values to a webpage and retrieving an answer
by nateg
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |