11
\$\begingroup\$

When one performs a search query using Google, one of the first things you see is an approximation of how many results were found. Today, you'll be simulating this in as few bytes as possible.

Input

A short string of length at most 10 with characters from a-z.

Output

The approx count that Google reports for the number of hits.

Test Cases

query string -> approximate number of results 
fish -> 2340000000
quantock -> 1390000
codegolf -> 891000
google -> 9340000000
e -> 22500000000
bzoea -> 34700
zoea -> 529000

Extra Clarifications

You may use any regional Google you like.

You can either scrape the page resulting from the search query, or interface with Google APIs.

Search queries are usually in the format https://www.google.com/query?q=<query>

This is , so the shortest code in bytes wins

\$\endgroup\$
9
  • \$\begingroup\$ Oh no! A downvote :( How come? \$\endgroup\$
    – Anush
    Jan 22 at 14:36
  • 2
    \$\begingroup\$ Is the idea of the challenge to actually query Google and return the exact value it gives? Not sure how you could estimate it. \$\endgroup\$
    – xnor
    Jan 22 at 14:40
  • 9
    \$\begingroup\$ I don't see why this is being close voted, it seems clear enough to me. \$\endgroup\$ Jan 22 at 14:41
  • 1
    \$\begingroup\$ Can we use Google's API or a third party tool rather than directly interacting with google.com? \$\endgroup\$
    – xnor
    Jan 22 at 14:51
  • 1
    \$\begingroup\$ Very similar challenge that required to compare two result counts. But due to some changes at Google most of the answers are now broken. \$\endgroup\$
    – ovs
    Jan 22 at 21:41
7
\$\begingroup\$

Python 3, 158 137 131 bytes

Using requests module and a doctored User-Agent header. Still looking for a shorter functioning UA header.

lambda q:get(f'http://goo.gl/search/{q}',headers={'User-Agent':'Firefox/8'}).text.split('out ')[1].split()[0]
from requests import*

Try it online!

\$\endgroup\$
14
  • \$\begingroup\$ This isn't an answer to the challenge. You need to be able to somehow input a string and return a number for starters. \$\endgroup\$
    – Noodle9
    Jan 23 at 7:47
  • \$\begingroup\$ Well, thanks for that. How do I do those things? I'm still finding my way around here. A string of digits is a number. And how on earth to test it on Try it online since it won't permit outgoing requests? Still figuring that bit out too. \$\endgroup\$
    – mhawke
    Jan 23 at 7:50
  • 2
    \$\begingroup\$ Oh, sorry didn't realise that. You can put your code in TIO like this. And then use the code-golf link to simply copy/paste it into your answer. Don't think it'll run properly there by it's does the formatting and byte count for you and makes it easier to look at your code. \$\endgroup\$
    – Noodle9
    Jan 23 at 9:12
  • 5
    \$\begingroup\$ @Noodle9 no worries. Thanks for the example. I didn't think of putting the import after the lamda def, that's pretty sneaky. It's going to take a while to adopt poor and bizarre programming practices. \$\endgroup\$
    – mhawke
    Jan 23 at 9:21
  • 1
    \$\begingroup\$ Firefox/8 is no longer a valid User-Agent \$\endgroup\$ Jul 4 at 19:47
5
\$\begingroup\$

Bash + curl + grep, 61

curl -LA Firefox/8 goo.gl/search?$1|grep -Po "bout \K[0-9,]*"

This will output curl's progress indicator to stderr. If you want to suppress that, change the -LA to -sLA.

\$\endgroup\$
3
  • \$\begingroup\$ Why Google.com and not Google.de for example? \$\endgroup\$
    – Anush
    Jan 25 at 5:22
  • 2
    \$\begingroup\$ @Anush yes that helps. goo.gl/search? is even better! \$\endgroup\$ Jan 25 at 16:52
  • 1
    \$\begingroup\$ Firefox/8 is no longer a valid User-Agent \$\endgroup\$ Jul 4 at 19:47
4
\$\begingroup\$

Applescript, 389

This is perhaps the most inefficient golfing language, but it is still fun to golf in:

open location"http://goo.gl/search?"&(display dialog""default answer"")'s text returned
repeat with l in{"a","c"}
delay 2
tell application"System Events"to keystroke l using command down
end
delay 1
set d to the clipboard
set Applescript's«class txdl»to" "
set c to d's text items
set p to""
repeat with w in c
considering case
if p contains"About"then display alert w
end
set p to w
end
\$\endgroup\$
3
\$\begingroup\$

PowerShell, 64 61 56 75 bytes

iwr goo.gl/search?$args -User Firefox/8|sls '(?<=">About )[\d,]*'|% M*|% V*

Try it online!

The correct solution is parseHtml method and xPath query div[@id="result-stats"] of course. But we are in CodeGolf! So let's look for a shorter code :)

The CodeGolf solution is based on the following:

  • the search string contains chars " and >
  • this chars are regular for regex and special for Html
  • any Html document should to escape this chars in value attributes and in the inner text - &quot; and &lt;
  • so we can found the string ">About only in a Raw content of a Html page.

Less golfed:

Invoke-WebRequest goo.gl/search?$args -UserAgent Firefox/8|
    Select-String '(?<=">About )[\d,]*'|% Matches|% Value

The script assumes the English locale is default.

The script returns a string same format as Google itself, So comma as thousand separators is fine. See comment from author Anush.

iwr is the alias for Invoke-WebRequest. M* is shortcut for the Matches method and V* is shortcut for the Value method.


  • Thanks @MarcMush for the brilliant question!
  • Thanks to mhawke for Mock for TIO idea.
\$\endgroup\$
4
  • 1
    \$\begingroup\$ does it still work for the search term bout ? \$\endgroup\$
    – MarcMush
    Jan 26 at 15:13
  • 1
    \$\begingroup\$ brilliant question! search term bout will be processed correctly. but here's the term bout 42! I need time to think. Thanks again! \$\endgroup\$
    – mazzy
    Jan 26 at 15:31
  • 2
    \$\begingroup\$ Firefox/8 is no longer a valid User-Agent \$\endgroup\$ Jul 4 at 19:48
  • 1
    \$\begingroup\$ Thanks. Let it be. Let it be. There will be an answer, let it be. \$\endgroup\$
    – mazzy
    Jul 4 at 21:06
2
\$\begingroup\$

Go, 260 bytes

Most answers on this page are now wrong, as Google no longer accepts Firefox/8 as a valid User-Agent:

package main
import(."io"
."net/http"
."regexp")
func main(){e,_:=NewRequest("GET","http://goo.gl/search?codegolf",nil)
e.Header.Set("User-Agent","Firefox/51")
s,_:=new(Client).Do(e)
b,_:=ReadAll(s.Body)
print(string(MustCompile(`About \S+ results`).Find(b)))}
\$\endgroup\$
2
  • \$\begingroup\$ How is 51 different from 8? \$\endgroup\$
    – Anush
    Jul 4 at 20:37
  • \$\begingroup\$ @Anush Google enforce higher version now \$\endgroup\$ Jul 4 at 20:39
1
\$\begingroup\$

05AB1E, 25 26 29 bytes

’Š¹.ŒŒ/€±?q=ÿ’žZì.w”€«”¡1è#1è

+1 byte as bug-fix if the input is about.
+3 bytes, because it apparently doesn't work without the www.

Outputs the result in the same format as on the Google page itself (so with dots for thousand separators). If it should be returned as an integer, an additional trailing þ can be added to the program.

Builtin .w (read data from an URL) is disabled on TIO, so the TIO's are split into two parts:

Creating the Google search URL based on the input:

’Š¹.ŒŒ/€±?q=ÿ’žZì

Try it online.

Get the amount of results from the Google page:

”€«”¡1è#1è

Try it online.
The input is only a small portion of the entire google page, since the TIO-link would become over 30,000 characters long with the entire page.. :/ To test any google search page with this part of the program, follow these steps:

  1. Ctrl+U (Show page source)
  2. Copy-paste everything into the second TIO's input-field
  3. Surround it with leading and trailing """ (for multi-line string inputs)

Explanation:

’Š¹.ŒŒ/€±?q=ÿ’    # Push dictionary string "google.com/search?q=ÿ"
                  # where the `ÿ` is automatically filled with the (implicit) input
  žZ              # Push builtin string "http://www."
    ì             # Prepend it in front of the string
     .w           # Go to this URL, and get its page as string
       ”€«”       # Push titlecased dictionary string "About"
           ¡      # Split the google page on that titlecased word
            1è    # Only leave the second part (0-based 1st)
              #   # Split that on spaces
               1è # And index into the second part (0-based 1st) to get the amount
                  # (after which the result is output implicitly)

See this 05AB1E tips (section How to use the dictionary?) to understand why ’Š¹.ŒŒ/€±?q=ÿ’ is "google.com/search?q=ÿ" and ”€«” is "About".

\$\endgroup\$
11
  • \$\begingroup\$ pastebin.com/sEeNLxeq seems to have expired already. \$\endgroup\$
    – Anush
    Jan 22 at 15:47
  • 2
    \$\begingroup\$ Have you tested this locally? Without prepending www. to the URL I get a 301 response that looks like this and the program prints http-equiv="content-type". ( With www. I get the mobile German site, but I guess that is client-dependent ;) ) \$\endgroup\$
    – ovs
    Jan 22 at 17:50
  • 2
    \$\begingroup\$ @Arnauld At least not on my system (what I linked above is the ouput of .w) \$\endgroup\$
    – ovs
    Jan 22 at 18:56
  • 2
    \$\begingroup\$ Unfortunately, I think you need to pass an explicit user-agent so that Google doesn't return the mobile page (cc @ovs). \$\endgroup\$
    – Arnauld
    Jan 22 at 20:05
  • 1
    \$\begingroup\$ I'm not sure "About" is guaranteed to work given the arbitrary A-Za-z strings that can appear, maybe ">About", "About " or ">About " is? \$\endgroup\$ Jan 23 at 12:51

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.