Mojo user agent

From Asenjo
Jump to: navigation, search


Mojolicious, the awesome Perl library to create web apps, has a built-in web browser that has a cli too: mojo.

There are other cli web browsers out there: good old wget, curl, even lynx with the dump switch. But what makes mojo so special is that you can parse web sites with it, even using jquery sintax.

A few examples will clarify my point.

  • get all links on the mojolicio.us site:
$ mojo get http://mojolicio.us a [enter]
<a href="http://github.com/kraih/mojo">
  <img alt="Fork me on GitHub" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" style="position: absolute; top: 0; right: 0; border: 0;">
</a>
<a href="http://latest.mojolicio.us">
    <img src="/unicorn.png">
</a>
<a href="http://mojolicio.us">
      <img alt="Mojolicious logo" src="/mojo/logo-white.png">
</a>
<a href="http://mojolicio.us/perldoc">Documentation</a>
<a href="https://github.com/kraih/mojo/wiki">Wiki</a>
<a href="https://github.com/kraih/mojo">GitHub</a>
<a href="http://metacpan.org/release/Mojolicious/">CPAN</a>
<a href="http://groups.google.com/group/mojolicious">MailingList</a>
<a href="http://blog.kraih.com">Blog</a>
<a href="http://twitter.com/kraih">Twitter</a>
<a href="http://metacpan.org/module/CGI">CGI</a>
<a href="perldoc/Mojolicious/Lite">Mojolicious::Lite</a>
<a href="http://plackperl.org">PSGI</a>
<a href="http://catalystframework.org">Catalyst</a>
<a href="http://perlbrew.pl">Perlbrew</a>
<a href="http://mojolicio.us/perldoc">documentation</a>
<a href="http://mojolicio.us">
    <img alt="Mojolicious logo" src="/mojo/logo-black.png">

Cool, but a bit too much html left for my taste. Dump that, please, I just want to see the text and not the links:

$ mojo get http://mojolicio.us a text
Documentation
Wiki
GitHub
CPAN
MailingList
Blog
Twitter
CGI
Mojolicious::Lite
PSGI
Catalyst
Perlbrew
documentation

Wait, I meant the href values:

$ mojo get http://mojolicio.us a attr href
http://github.com/kraih/mojo
http://latest.mojolicio.us
http://mojolicio.us
http://mojolicio.us/perldoc
https://github.com/kraih/mojo/wiki
https://github.com/kraih/mojo
http://metacpan.org/release/Mojolicious/
http://groups.google.com/group/mojolicious
http://blog.kraih.com
http://twitter.com/kraih
http://metacpan.org/module/CGI
perldoc/Mojolicious/Lite
http://plackperl.org
http://catalystframework.org
http://perlbrew.pl
http://mojolicio.us/perldoc
http://mojolicio.us

Or maybe I just want to see links to secure sites:

$ mojo get http://mojolicio.us a[href^="https"]
<a href="https://github.com/kraih/mojo/wiki">Wiki</a>
<a href="https://github.com/kraih/mojo">GitHub</a>

mojo makes parsing html fun :-)