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



Might return something like:

<!DOCTYPE html>
    <title>Cats are cool</title>
    <meta charset="utf-8" />
      Cats are so cool!

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.

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

Ruby, 79 / 145

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


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


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
function l($q){
$ch = curl_init("https://google.ca/search?btnI=1&q=" . rawurlencode($q));
curl_setopt_array($ch, array(
return curl_exec($ch);
  • \$\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\$ – NinjaBearMonkey Oct 13 '14 at 23:00
  • \$\begingroup\$ Just realized that, changing it for both my answers \$\endgroup\$ – rink.attendant.6 Oct 13 '14 at 23:01
  • \$\begingroup\$ Are you using SSL for any particular reason? \$\endgroup\$ – Dennis Oct 14 '14 at 2:14
  • \$\begingroup\$ You can replace btnI=1 with btnI. \$\endgroup\$ – Beta Decay Oct 19 '14 at 8:48

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)));}


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)
  • 1
    \$\begingroup\$ You can just use 'DownloadString' and avoid the Encoding bit. \$\endgroup\$ – Geoff Oct 20 '14 at 7:33

CJam, 40 bytes

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


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

  • \$\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 Oct 19 '14 at 18:47
  • \$\begingroup\$ @aditsu - Any plans for that ? \$\endgroup\$ – Optimizer Oct 19 '14 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 Oct 20 '14 at 1:24

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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