8
\$\begingroup\$

Write the shortest function that returns the content of the first result of a Google search ("I'm feeling lucky").

Example:

lucky('cat');

Might return something like:

<!DOCTYPE html>
<html>
  <head>
    <title>Cats are cool</title>
    <meta charset="utf-8" />
  </head>
  <body>
    <h1>CAAAAAAAAAAATSSSSSSSSSS!!!!!!!!</h1>
    <p>
      Cats are so cool!
    </p>
  </body>
</html>
\$\endgroup\$
0

6 Answers 6

12
\$\begingroup\$

Bash, 46 bytes

l()(wget -UM -qO- "gogle.de/search?btnI&q=$1")

Uses the actual I'm feeling lucky feature, just like the other answers.

Gogle blacklists some user agents (including WGet's, cURL's and Java's), but M seems to work just fine.

\$\endgroup\$
4
  • \$\begingroup\$ Why does gogle.de even exist? \$\endgroup\$
    – user16402
    Commented Oct 15, 2014 at 20:14
  • 3
    \$\begingroup\$ To catch typos. googl.de would work as well. \$\endgroup\$
    – Dennis
    Commented Oct 15, 2014 at 20:24
  • 2
    \$\begingroup\$ @professorfish gogle.com exists too. Unfortunatly googl.co.uk redirects to a dodgy ad site. \$\endgroup\$
    – Beta Decay
    Commented Oct 19, 2014 at 7:30
  • \$\begingroup\$ @BetaDecay Curiously, it redirects me to a different page every time. \$\endgroup\$
    – qwr
    Commented Oct 19, 2014 at 19:53
7
\$\begingroup\$

Ruby, 79 / 145

I borrowed the btnI trick from @rink.attendant.6 for this solution. Thanks Beta Decay for shortening it by 2 characters.

require'open-uri'
f=->q{URI(URI.escape"http://gogle.de/search?btnI&q="+q).read}

I also have a solution which actually gets the first result from the results page, which is 145 bytes.

require'open-uri'
f=->q{open(URI.extract(URI(URI.escape"http://google.com/search?q=#{q}").read.split('class="r"')[1])[0].split("&amp;")[0]).read}
\$\endgroup\$
0
5
\$\begingroup\$

PHP, 157 105/87 102/87 100/85

Using file_get_contents

<?php function l($q){return file_get_contents("https://google.ca/search?btnI&q=".rawurlencode($q));}

Without allowing for spaces in the search term it's only 87 characters:

<?php function l($q){return file_get_contents("https://google.ca/search?q=$q&btnI");}

Original versions using cURL

I suppose that short array syntax can be used on PHP 5.4+:

<?php function l($q){$ch=curl_init("https://google.ca/search?btnI=1&q=".rawurlencode($q));curl_setopt_array($ch,[19913=>1,52=>1]);return curl_exec($ch);}

Otherwise it's five more characters with the normal array initializer, 162:

<?php function l($q){$ch=curl_init("https://google.ca/search?btnI=1&q=".rawurlencode($q));curl_setopt_array($ch,[19913=>1,52=>1]);return curl_exec($ch);}

Version that does not allow spaces in the search term: No need for URL encoding (138):

<?php function l($q){$ch=curl_init("https://google.ca/search?q=$q&btnI=1");curl_setopt_array($ch,[19913=>1,52=>1]);return curl_exec($ch);}
Ungolfed using constants
<?php
function l($q){
$ch = curl_init("https://google.ca/search?btnI=1&q=" . rawurlencode($q));
curl_setopt_array($ch, array(
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_FOLLOWLOCATION => 1
  ));
return curl_exec($ch);
}
\$\endgroup\$
4
  • \$\begingroup\$ You should be able to replace "https://google.ca/search?q=".rawurlencode($q)."&btnI=1" with "http://google.ca/search?btnI=1&q=".rawurlencode($q) \$\endgroup\$ Commented Oct 13, 2014 at 23:00
  • \$\begingroup\$ Just realized that, changing it for both my answers \$\endgroup\$ Commented Oct 13, 2014 at 23:01
  • \$\begingroup\$ Are you using SSL for any particular reason? \$\endgroup\$
    – Dennis
    Commented Oct 14, 2014 at 2:14
  • \$\begingroup\$ You can replace btnI=1 with btnI. \$\endgroup\$
    – Beta Decay
    Commented Oct 19, 2014 at 8:48
4
\$\begingroup\$

C#, 183 180 178

This is my first time ever writing code in C# so it could probably use improvement. Feedback is welcome!

string l(string q){return System.Text.Encoding.UTF8.GetString((new System.Net.WebClient()).DownloadData("https://google.ca/search?btnI&q="+System.Web.HttpUtility.UrlEncode(q)));}

Unminified

string l(string q) {
    return System.Text.Encoding.UTF8.GetString(
        (new System.Net.WebClient()).DownloadData(
            "https://google.ca/search?btnI&q=" + System.Web.HttpUtility.UrlEncode(q)
        )
    );
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can just use 'DownloadString' and avoid the Encoding bit. \$\endgroup\$
    – Geoff
    Commented Oct 20, 2014 at 7:33
3
\$\begingroup\$

CJam, 40 bytes

Thought, I'll finally give the g method a try

{"gogle.de/search?btnI&q="\S/"%20"*+g}:F

This creates a method/block F which can be used like

{"gogle.de/search?btnI&q="\S/"%20"*+g}:F; "cats and dogs"F

This is how functions work in CJam ..

Doesn't work in online interpreter, so you will have to download and use the Java one.

Note that Google denies all requests with Java user agent, so you will have to start CJam with an additional flag -Dhttp.agent=M

\$\endgroup\$
3
  • \$\begingroup\$ +1 for CJam and you, -1 for Google. They return 403 if the User-Agent is Java. I hope @aditsu will solve this. \$\endgroup\$
    – manatwork
    Commented Oct 19, 2014 at 18:47
  • \$\begingroup\$ @aditsu - Any plans for that ? \$\endgroup\$
    – Optimizer
    Commented Oct 19, 2014 at 18:58
  • 2
    \$\begingroup\$ This will only work if you spoof the user agent by passing, e.g., -Dhttp.agent=M to the Java interpreter. \$\endgroup\$
    – Dennis
    Commented Oct 20, 2014 at 1:24
2
\$\begingroup\$

Python 3 - 78

Uses gogle.de for brevity. Run as f(query).

f=lambda x:__import__("requests").get("http://gogle.de/search?btnI&q="+x).text

If you want to have spaces in your query it's 98 characters.

f=lambda x:__import__("requests").get("http://gogle.de/search?btnI&q="+x.replace(" "," %20")).text
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.