Code Golf : Parsing google results

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 :

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.

• Since you are using google.fr, do we have to use that as well? – Beta Decay Jul 31 '15 at 12:31
• You can use any google you want. I'm french, so I used .fr, but you could use .com or .anything :) Dosn't matter – WayToDoor Jul 31 '15 at 12:37
• And shortened URLs such as gogle.de are fine as well? – Beta Decay Jul 31 '15 at 12:40
• 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 – WayToDoor Jul 31 '15 at 12:41
• In case you're tempted: remember you can't parse HTML with regex – Luis Mendo Jul 31 '15 at 15:16

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

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]}

• 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.) – manatwork Jul 31 '15 at 19:15
• Are you sure you need to explicitly require'uri'? In 2.1.2 I use the URI module becomes available after requiring open-uri. – manatwork Jul 31 '15 at 19:24
• @manatwork Thank you! Updated. – kirbyfan64sos Jul 31 '15 at 20:16
• 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.) – manatwork Aug 1 '15 at 10:30
• @manatwork I need to, but I was too lazy to figure out the byte count at the moment. :) – kirbyfan64sos Aug 1 '15 at 14:56

Wolfram Language (Mathematica), 135

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


StringJoin/@(Cases[
XMLElement["cite",_,l_]:>l,-1] /.
XMLElement["b",_,{s_}]:>s)

• Are the spaces really necessary? Without them, I get 136 bytes. – kirbyfan64sos Jul 31 '15 at 20:18
• not necessary at all...I really should tighten this up.. – chuy Jul 31 '15 at 20:20
• Can you do something like this answer to shorten this? – Digital Trauma Jul 31 '15 at 20:26

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://www.crunchbase.com/organization/
https://github.com/


Implements grc's tip

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

Factor, 31 bytes

There happens to be a library for this.

[ google-search [ url>> ] map ]