Welcome, guest | Sign In | My Account | Store | Cart

Sometimes you need o know the number of results that google gets for a specific query. There are lots of scripts that claim to do that, but I didn't find any that worked. They mostly rely on urllib, which is blocked by Google.

This script automates what you would do by hand. It is therefore incredibly slow, but it works and seems future proof to me.

You may want to adjust the respective timespan that is waited before certain operations. This script relies on the unix command xsel. I'm sure there are equivalent solutions on other operating systems.

Python, 100 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
import autopy
from time import sleep

import subprocess
import re

m = autopy.mouse
k = autopy.key


class count_detector:

    CORRECTED_MSG = 'Showing results for'
    #a comma seperated number
    NUMBER = re.compile(r'^[\d]{1,3}(,[\d]{3})*$')

    def calibrate(self):
        #determine position of search field:
        raw_input('please move cursor over search field and press enter.')
        self.search_field = m.get_pos()

        #determine position of result count
        print("Search something google will find.")
        raw_input('Then move the cursor over the count and press enter.')
        self.result = m.get_pos()

        #Google might autocorrect the query foo to ba.
        #In this case we need to press 'Search instead for foo'
        #in order to get the right count
        print("Search something google will autocorrect.")
        raw_input("Move cursor slightly left to 'Showing results for ' and press enter.")
        self.autocorrect_indicator_left = m.get_pos()

        raw_input("Move cursor to the right of whole 'Showing results for '-thing and press enter.")
        self.autocorrect_indicator_right = m.get_pos()

        #adjust right point. we need about same y-value and the one from left point is death sure
        self.autocorrect_indicator_right = (self.autocorrect_indicator_right[0],
                                            self.autocorrect_indicator_left[1])

        raw_input("Move cursor over 'Search instead for '-link and press enter")
        self.deautocorrect = m.get_pos()

    def get_count(self, word):
        #get focus
        m.move(*self.search_field)

        #select old
        m.click()
        m.click()
        m.click()

        sleep(5)

        #enter new
        k.type_string(word, 100)
        k.tap(k.K_RETURN)

        #get result count
        count = self.__fetch_results(1, 2)

        if not self.__is_count(count):  # if field is blank -> no entrys
            count = '0'
        else:

            #check for possible autocorrect
            #select possible 'Showing results for' message and examine
            m.move(*self.autocorrect_indicator_left)
            sleep(1)
            m.toggle(True)
            m.move(*self.autocorrect_indicator_right)
            m.toggle(False)

            string = self.__get_clipboard(2)

            if string.find(self.CORRECTED_MSG) == 0:
                #if query was autocorrected, deautocorrect
                m.move(*self.deautocorrect)
                m.click()
                count = self.__fetch_results(2, 2)

        return count

    def __fetch_results(self, waiting_time, waiting_time2):
        sleep(waiting_time)
        m.move(*self.result)
        m.click()
        m.click()
        return self.__get_clipboard(waiting_time2)

    def __get_clipboard(self, waiting_time):
        sleep(waiting_time)

        child = subprocess.Popen('xsel', stdout=subprocess.PIPE)
        count = child.communicate()
        return count[0]

    #determine if scrapped count is valid to sort out 'nothing found' and stuff
    def __is_count(self, number_to_test):
        return self.NUMBER.match(number_to_test)

1 comment

Varun Chhangani 9 years, 11 months ago  # | flag

any one can describe this code ???