views:

428

answers:

12

Often times I want to automate http queries. I currently use Java(and commons http client), but would probably prefer a scripting based approach. Something really quick and simple. Where I can set a header, go to a page and not worry about setting up the entire OO lifecycle, setting each header, calling up an html parser... I am looking for a solution in ANY language, preferable scripting

A: 

What about using PHP+Curl, or just bash?

Mr-sk
+2  A: 

Python urllib may be what you're looking for.

Alternatively powershell exposes the full .NET http library in a scripting environment.

John Weldon
Often, folks need urllib2 more than urllib.
S.Lott
+4  A: 

Mechanize for Python seems easy to use: http://wwwsearch.sourceforge.net/mechanize/

emil
Mechanize for Ruby also works well.
Wayne Conrad
It also seems to exist for perl as well.
emil
+6  A: 

Have a look at Selenium. It generates code for C#, Java, Perl, PHP, Python, and Ruby if you need to customize the script.

jbochi
Of the code that is generated, what http libraries do they use? The default ones or ones like mechanize?
Zombies
Selenium has a wrapper library for each one the languages.
jbochi
As far as I understand Selenium, it uses a fully flegded browser like firefox. This seems to me like killing a mouse with a nuclear bomb.
johannes
Hardy johannes. It's a using a browser (which is the most common way to use web apps) to test web apps (which are most commonly accessed through a browser).
Noufal Ibrahim
I tried Selenium. I was considering using it initially build a script and then edit it. Here is an example of the API: @selenium.click "link=Yahoo!". To me this is problematic if the link changes or if the link is dynamically generated.
Zombies
+6  A: 

Watir sounds close to what you want although it (like Selenium linked to in another answer) actually opens up a browser to do stuff. You can see some examples here. Another browser based record + playback approach system is sahi.

If your application is uses WSGI, then paste is a nice option.

Mechanize linked to in another answer is a "browser in a library" and there are clones in perl, Ruby and Python. The Perl one is the original one and this seems to be the way to go if you don't want a browser. The problem with this approach is that all the front end code (which might rely on JavaScript), won't be exercised.

Noufal Ibrahim
Does httpunit for java execute JavaScript?
Zombies
Also, when it opens a browser.... does it become an active window and manipulate clicks/events?
Zombies
Never used it. Unless there's a browser involved (or it loads up a JS interpreter somehow), I don't think it will.
Noufal Ibrahim
It's hard for me to describe how the browser based systems work. The Selenium homepage has a few screencasts. If you view them, I'm sure you'll understand.
Noufal Ibrahim
@Zombies: httpunit supports some javascript, see http://www.httpunit.org/doc/javascript-support.html
stephan
A: 

Some ruby libraries:

  • httparty: really interesting, the philosophy is interesting.
  • mechanize: classic good-quality web automatization library.
  • scrubYt: puzzling at first glance but fun to use.
paradigmatic
+6  A: 

My turn : wget or perl with lwp. You'll find example on the linked page.

Aif
+3  A: 

Depending on exactly what you're doing the easiest solution looks to be bash + curl.

The man page for the latter is available here:

http://curl.haxx.se/docs/manpage.html

You can do posts as well as gets, HTTPS, show headers, work with cookies, basic and digest HTTP authentication, tunnel through all sorts of proxies, including NTLM on *nix amongst other things.

curl is also available as shared library with C and PHP support.

HTH

C.

symcbean
+4  A: 

I'm testing ReST APIs at the moment and found the ReST Client very nice. It's a GUI program, but nonetheless you can save and restore queries as XML files (or let them be generated), embed, write test scripts, and so on. And it's Java based (which is not an ad-hoc advantage, but you mentioned it).

Minus points for recording sessions. The ReST Client is good for stateless "one-shots".

If it doesn't suit your needs, I'd go for the already mentioned Mechanize (or WWW-Mechanize, as it is called at CPAN).

Boldewyn
+2  A: 

Twill is pretty good and made for testing. It can be used as script, in an interactive session or within a Python program.

ars
+6  A: 

If you have simple needs (fetch a page and then parse it), it is hard to beat LWP::Simple and HTML::TreeBuilder.

use strict;
use warnings;

use LWP::Simple;
use HTML::TreeBuilder;

my $url = 'http://www.example.com';
my $content = get( $url) or die "Couldn't get $url";

my $t = HTML::TreeBuilder->new_from_content( $content );
$t->eof;
$t->elementify;

# Get first match:
my $thing = $t->look_down( _tag => 'p', id => qr/match_this_regex/ );

print $thing ? $thing->as_text : "No match found\n";

# Get all matches:
my @things = $t->look_down( _tag => 'p', id => qr/match_this_regex/ );

print $_ ? $_->as_text : "No match found" for @things;
daotoad
Yup, LWP and HTML::TreeBuilder usually go together.
Leonardo Herrera
@Leonardo, like chocolate and peanut butter--good on their own, but better together.
daotoad
+2  A: 

Perl and WWW::Mechanize can make web scraping etc simple and easy, including easy handling of forms (let's say you want to go to a login page, fill in a username and password and submit the form, handling cookies / hidden session identifiers just as a browser would...)

Similarly, finding or extracting links from the fetched page is trivial.

If you need to parse stuff out of the resulting pages that WWW::Mechanize can't easily help with, then feed the result to HTML::TreeBuilder to make parsing easy.

David Precious