jarun / googler

:mag: Google from the terminal
GNU General Public License v3.0
6.09k stars 529 forks source link

Number of search results #144

Closed mrquincle closed 7 years ago

mrquincle commented 7 years ago

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 commented 7 years ago

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

zmwangx commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 6 years ago

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 commented 6 years ago

Are you running on Mac OSx? -P is no longer supported ᐧ

On Mon, Jan 22, 2018 at 1:09 PM, Rishabh Tayal notifications@github.com wrote:

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

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jarun/googler/issues/144#issuecomment-359513155, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHJy5bJgSD37jGUqNnrCZtXngZADcZJks5tNM7IgaJpZM4Kyywn .

zmwangx commented 6 years ago

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

fredzannarbor commented 6 years ago

Meant to include this page

https://stackoverflow.com/questions/16658333/grep-p-no-longer-works-how-can-i-rewrite-my-searches

On Mon, Jan 22, 2018 at 1:23 PM, Zhiming Wang notifications@github.com wrote:

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jarun/googler/issues/144#issuecomment-359517289, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHJy9ExnmFwT2T3sA2vqYxXh4ba4rOiks5tNNILgaJpZM4Kyywn .

RishabhTayal commented 6 years ago

@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 commented 6 years ago

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

zmwangx commented 6 years ago

@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).