17
\$\begingroup\$

When you search something on google, within the results page, the user can see green links, for the first page of results.

In the shortest form possible, in bytes, using any language, display those links to stdout in the form of a list. Here is an example, for the first results of the stack exchange query :

A screen capture

Input :

you choose : the URL (www.google.com/search?q=stackexchange&ie=utf-8&oe=utf-8) or just stackexchange

Output :

french.stackexchange.com/, stackoverflow.com/, fr.wikipedia.org/wiki/Stack_Exchange_Network, en.wikipedia.org/wiki/Stack_Exchange,...

Rules :

  • You may use URL shorteners or other search tools/APIs as long as the results would be the same as searching https://www.google.com.

  • It's ok if your program has side effects like opening a web browser so the cryptic Google html/js pages can be read as they are rendered.

  • You may use browser plugins, userscripts...

  • If you can't use stdout, print it to the screen with, eg. a popup or javascript alert !

  • You don't need the ending / or the starting http(s)://

  • You should not show any other link

  • Shortest code wins !

  • Good luck !

EDIT : This golf ends the 07/08/15.

\$\endgroup\$
11
  • \$\begingroup\$ Since you are using google.fr, do we have to use that as well? \$\endgroup\$
    – Beta Decay
    Jul 31, 2015 at 12:31
  • \$\begingroup\$ You can use any google you want. I'm french, so I used .fr, but you could use .com or .anything :) Dosn't matter \$\endgroup\$
    – WayToDoor
    Jul 31, 2015 at 12:37
  • \$\begingroup\$ And shortened URLs such as gogle.de are fine as well? \$\endgroup\$
    – Beta Decay
    Jul 31, 2015 at 12:40
  • \$\begingroup\$ You may use URL shorteners or other search tools/APIs as long as the results would be the same as searching google.com, so yes \$\endgroup\$
    – WayToDoor
    Jul 31, 2015 at 12:41
  • 7
    \$\begingroup\$ In case you're tempted: remember you can't parse HTML with regex \$\endgroup\$
    – Luis Mendo
    Jul 31, 2015 at 15:16

5 Answers 5

17
\$\begingroup\$

Bash + grep + lynx, 38

Since we can open a web browser, then I will use lynx:

lynx -dump $1|grep -Po '(?<=d:)[^&]+'

(Thanks to @manatwork for grep usage instead of sed)

We pass in the whole URL in as a parameter:

$ ./gr.sh "www.google.com/search?q=stackexchange&ie=utf-8&oe=utf-8"
http://stackexchange.com/
https://en.wikipedia.org/wiki/Stack_Exchange
https://twitter.com/stackexchange
https://play.google.com/store/apps/details?id=com.stackexchange.marvin
https://github.com/StackExchange/StackExchange.Redis
https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/Basics.md
https://www.crunchbase.com/organization/stack-exchange
$ 

Which gives the same list as:

enter image description here

\$\endgroup\$
7
  • \$\begingroup\$ Well that's handy :D \$\endgroup\$
    – Beta Decay
    Jul 31, 2015 at 16:35
  • 3
    \$\begingroup\$ sed good. sed long. Try GNU grep: grep -Po '(?<=d:)[^&]+' \$\endgroup\$
    – manatwork
    Jul 31, 2015 at 16:35
  • \$\begingroup\$ @manatwork Yes, of course - thanks! \$\endgroup\$ Jul 31, 2015 at 16:40
  • 1
    \$\begingroup\$ Was the answer title copypasted? ;) None of bash, lynx or sed (and now grep) is part of coreutils. \$\endgroup\$
    – manatwork
    Jul 31, 2015 at 16:42
  • 3
    \$\begingroup\$ I believe you can also do: lynx -dump $1|grep -Po 'd:\K[^&]+' (untested) \$\endgroup\$
    – Jarmex
    Aug 1, 2015 at 10:13
4
\$\begingroup\$

Ruby, 91 77 bytes

require'open-uri';open(gets).read.scan(/ed:(.*?)\+/){|x|puts URI.decode x[0]}

Would've been shorter without all the requires. ARGH!!! EDIT: So, turns out, I don't need the second require! Thanks to @manatwork for pointing that out.

Older version (with the useless require):

require'open-uri';require 'uri';open(gets).read.scan(/ed:(.*?)\+/){|x|puts URI.decode x[0]}
\$\endgroup\$
5
  • \$\begingroup\$ Rules allow the use of command line options as long as you count them too: pastebin.com/PnpjnXji (If you feel this is unfair style, feel free to use only the change in the code block.) \$\endgroup\$
    – manatwork
    Jul 31, 2015 at 19:15
  • \$\begingroup\$ Are you sure you need to explicitly require'uri'? In 2.1.2 I use the URI module becomes available after requiring open-uri. \$\endgroup\$
    – manatwork
    Jul 31, 2015 at 19:24
  • \$\begingroup\$ @manatwork Thank you! Updated. \$\endgroup\$ Jul 31, 2015 at 20:16
  • \$\begingroup\$ Just for my curiosity: any reason to not change the code block as in my pastebin alternative? (Of course, I'm curious about technical reasons, not personal reasons, if that hold you back.) \$\endgroup\$
    – manatwork
    Aug 1, 2015 at 10:30
  • \$\begingroup\$ @manatwork I need to, but I was too lazy to figure out the byte count at the moment. :) \$\endgroup\$ Aug 1, 2015 at 14:56
4
\$\begingroup\$

Wolfram Language (Mathematica), 135

StringJoin/@(Cases[URLExecute["www.google.com/search",{"q"->#},"XMLObject"],XMLElement["cite",_,l_]:>l,-1]/.XMLElement["b",_,{s_}]:>s)&

more readable:

StringJoin/@(Cases[
    URLExecute["www.google.com/search",{"q"->#},"XMLObject"], 
    XMLElement["cite",_,l_]:>l,-1] /. 
    XMLElement["b",_,{s_}]:>s)
\$\endgroup\$
3
  • \$\begingroup\$ Are the spaces really necessary? Without them, I get 136 bytes. \$\endgroup\$ Jul 31, 2015 at 20:18
  • \$\begingroup\$ not necessary at all...I really should tighten this up.. \$\endgroup\$
    – chuy
    Jul 31, 2015 at 20:20
  • \$\begingroup\$ Can you do something like this answer to shorten this? \$\endgroup\$ Jul 31, 2015 at 20:26
3
\$\begingroup\$

Python 3, 141 bytes

Nowhere near Digital Trauma's answer, but it was fun to work out the regex :D

import re
print('\n'.join(map(lambda x:x[3:],re.findall('te>http[s]?://\w+\.[a-z]+[](/a-z\.)?]+',__import__("requests").get(input()).text))))

For input http://www.google.com/search?q=stackexchange&ie=utf-8&oe=utf-8 the program outputs:

https://en.wikipedia.org/wiki/
https://twitter.com/
https://play.google.com/store/apps/details?id...
https://www.crunchbase.com/organization/
https://www.facebook.com/
https://github.com/

Implements grc's tip

\$\endgroup\$
2
  • \$\begingroup\$ Do you really need to use __import__? \$\endgroup\$
    – ckjbgames
    May 31, 2017 at 0:27
  • \$\begingroup\$ Also, use an [x for x in spam] construct instead of map. That will save you a good number of bytes. \$\endgroup\$
    – ckjbgames
    May 31, 2017 at 1:14
3
\$\begingroup\$

Factor, 31 bytes

There happens to be a library for this.

[ google-search [ url>> ] map ]
\$\endgroup\$

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.