Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A very nice interface for this type of work is provided by some tools of the <a href="http://mojolicio.us" rel="nofollow"><strong>Mojolicious</strong></a> distribution.</p> <h1>Long version</h1> <p>The combination of its <a href="http://mojolicio.us/perldoc/Mojo/UserAgent" rel="nofollow">UserAgent</a>, <a href="http://mojolicio.us/perldoc/Mojo/DOM" rel="nofollow">DOM</a> and <a href="http://mojolicio.us/perldoc/Mojo/URL" rel="nofollow">URL</a> classes can work in a very robust way:</p> <pre><code>#!/usr/bin/env perl use strict; use warnings; use feature 'say'; use Mojo::UserAgent; use Mojo::URL; # preparations my $ua = Mojo::UserAgent-&gt;new; my $url = "http://www.imdb.com/search/title?title=Casino%20Royale"; # try to load the page my $tx = $ua-&gt;get($url); # error handling die join ', ' =&gt; $tx-&gt;error unless $tx-&gt;success; # extract the url my $movie_link = $tx-&gt;res-&gt;dom('a[href^=/title]')-&gt;first; my $movie_url = Mojo::URL-&gt;new($movie_link-&gt;attrs('href')); say $movie_url-&gt;path-&gt;parts-&gt;[-1]; </code></pre> <p>Output:</p> <pre><code>tt0381061 </code></pre> <h1>Short version</h1> <p>The funny one liner helper module <a href="http://mojolicio.us/perldoc/ojo" rel="nofollow">ojo</a> helps to build a very short version:</p> <pre><code>$ perl -Mojo -E 'say g("imdb.com/search/title?title=Casino%20Royale")-&gt;dom("a[href^=/title]")-&gt;first-&gt;attrs("href") =~ m|([^/]+)/?$|' </code></pre> <p>Output:</p> <pre><code>tt0381061 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload