Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

Number of search results #144

Closed
mrquincle opened this issue Nov 15, 2016 · 12 comments
Closed

Number of search results #144

mrquincle opened this issue Nov 15, 2016 · 12 comments

Comments

@mrquincle
Copy link

A feature request.

Is it possible to list the number of search results in the output? I'm often using it to define how popular a brand is. And as a non-native speaker on how to spell a word. :-)

Google always displays this as something like "About 2.620.000.000 results (0,53 seconds)" which is never accurate, but good enough for me.

@jarun
Copy link
Owner

jarun commented Nov 15, 2016

Would you mind raising a PR? And this should show just above the omniprompt only in the first page of a search.

@zmwangx
Copy link
Collaborator

zmwangx commented Nov 15, 2016

I recall thinking about this and decided it was not worth the complexity.

I'm often using it to define how popular a brand is.

Instead of requesting a feature from googler then parsing it from googler's output, here's a much simpler and much more direct approach — a one-liner shell script (assuming English language and GNU grep; easy to adapt otherwise):

$ cat google-result-count
#!/bin/sh
curl -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36" -sSL "https://www.google.com/search?q=$1" | grep -Po "(?<=About )[0-9,]+(?= results)" | tr -d ,
$ ./google-result-count google
10520000000
$ ./google-result-count apple
2120000000
$ ./google-result-count microsoft
1080000000

Feel free to contribute the feature though.

@zmwangx
Copy link
Collaborator

zmwangx commented Nov 15, 2016

By the way here's a more robust version with proper quoting and supporting multiple arguments:

#!/bin/sh
for keyword; do
    safe_keyword="$(python3 -c 'import urllib.parse, sys; print(urllib.parse.quote_plus(sys.argv[1]))' \""$keyword"\")"
    curl -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36" -sSL "https://www.google.com/search?q=$safe_keyword" | grep -Po "(?<=About )[0-9,]+(?= results)" | tr -d ,
done
$ ./google-result-count google apple microsoft 'western digital'
10770000000
2170000000
1100000000
20200000

@mrquincle
Copy link
Author

I decided to have a few lines in bash indeed, wget with user agent string works as well. If it's complex to add to googler itself, don't do it of course! It's just a feature request.

@jarun
Copy link
Owner

jarun commented Nov 17, 2016

I investigated the case today and the parser does need to go through some hacky modifications OR some extra parsing per page (to avoid, again, hacks). I believe we can safely avoid this. Closing the issue.

@RishabhTayal
Copy link

What does -Po option do in grep? I am getting error when running the script:

#!/bin/sh
for keyword; do
    safe_keyword="$(python3 -c 'import urllib.parse, sys; print(urllib.parse.quote_plus(sys.argv[1]))' \""$keyword"\")"
    curl -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36" -sSL "https://www.google.com/search?q=$safe_keyword" | grep -Po "(?<=About )[0-9,]+(?= results)" | tr -d ,
done

@fredzannarbor
Copy link

fredzannarbor commented Jan 22, 2018 via email

@zmwangx
Copy link
Collaborator

zmwangx commented Jan 22, 2018

-P (for PCRE) was never supported by BSD grep. Read the man page of GNU grep.

@fredzannarbor
Copy link

fredzannarbor commented Jan 22, 2018 via email

@RishabhTayal
Copy link

RishabhTayal commented Jan 22, 2018

@fredzannarbor I am running on Mac OSX. I am not a shell expert or expert with grep. What is the solution for the error? What is the replacement for -P?

@kenorb
Copy link

kenorb commented Jan 22, 2018

@RishabhTayal Try creating a different new issue, this one is closed and not related to yours.

@zmwangx
Copy link
Collaborator

zmwangx commented Jan 22, 2018

@RishabhTayal Use sed:

curl -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36" -sSL "https://www.google.com/search?q=hello" | \
  /usr/bin/sed -nE 's/.*About ([0-9,]+) results.*/\1/p'

or perl:

curl -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36" -sSL "https://www.google.com/search?q=hello" | \
  perl -nle 'print "$1" if /(?<=About )([0-9,]+)(?= results)/'

or even a double grep:

curl -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36" -sSL "https://www.google.com/search?q=hello" | \
   grep -Eo 'About [0-9,]+ results' | grep -Eo '[0-9,]+'

Of course there's also gawk, but I'm not sure /usr/bin/awk on macOS supports capture groups.

Seriously though, this is not a googler issue, and the issue tracker is not a discussion forum. This kind of questions should be directly to StackOverflow. If you want to learn text editing with grep, sed, awk, etc., there are plenty of good starting points, a few random ones off the top of my head:

http://www.grymoire.com/Unix/sed.html
http://www.grymoire.com/Unix/Awk.html
http://matt.might.net/articles/sculpting-text/
https://ferd.ca/awk-in-20-minutes.html

And I don't need to introduce Perl resources (maybe start with The Llama Book).

@lock lock bot locked and limited conversation to collaborators Nov 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants