GoogleScraper.py - A simple python module to parse google search results.

Posted on So 06 Januar 2013 in Programming

UPDATE on 18th February 2014:

This python module has now its own github repository!

The plugin can extract

  • All links
  • Link titles
  • The description/caption below the links

and has the following features:

  • Advanced proxy support for SOCKS4/4a/5 and HTTP PROXY
  • Multithreading
  • XPATH parsing
  • Supports almost all search parameters

Please note that this is by no means a permanent version! Heavy structural changes will be implemented in the near future (I'll experiment with asynchronous networking for instance). But on this site, I will always host a working version with instructions how to use it, such that visitors can always use the script!

1. Edit (07.01.2013):

  • Using requests instead of urllib
  • Added random User Agents for every new search.
  • Cleaned the code
  • Implemented foundation to combine with proxychains

Original Blog Post

Sample output after searching for 'cats are not cute' (sorry) with 100 results per page on 3 ascending pages: results.txt

I always was in need of a fast and reliable working python module to query the google search engine. The google API is rubbish, because they just give you maximally 36 results. This is completly inacceptable!

So, I looked further and found http://code.google.com/p/pygoogle/, which is not what we want. They say:

pygoogle is a very basic Google search module for Python. It has a limitation of only 64 results. If you want more results, see xgoogle.

Le optimistic me goes to the homepage of xgoogle, just to be disappointed again. The module seems broken and imports some outdated libraries and is generally very very large. The author probably did a nice job, but I immediately realized that I have to implement my own. This was unacceptable, since my python programming knowledge and coding style really lacks any depth and profundity.

The module should basically satisfy two purposes:

  • Parse arbitrary number of pages with maximally number of search results per page.
  • Clean the found urls from badboys like 'gstatic.com' or 'google.com'

What can you use the module for?

  • Statistics
  • Find vulnerable applications
  • scanning lots of sites with dorks (with intext, inurl, site parameters usable in the query!)

To-do list (06.01.2013):

  • Clean the code. Treat all errors correctly and make the script more robust.
  • Add functionality, like a parallel yahoo and bing search to compare the search results to gain maximal knowledge!
  • Provide better configuration freedom. There are a lot of google search parameters :/
  • Maybe: Port to Python 2.7

Usage

import GoogleScraper
import urllib.parse

if __name__ == '__main__':

    results = GoogleScraper.scrape('Best SEO tool', num_results_per_page=50, num_pages=3, offset=0)
    for page in results:
        for link_title, link_snippet, link_url in page['results']:
            # You can access all parts of the search results like that
            # link_url.scheme => URL scheme specifier (Ex: 'http')
            # link_url.netloc => Network location part (Ex: 'www.python.org')
            # link_url.path => URL scheme specifier (Ex: ''help/Python.html'')
            # link_url.params => Parameters for last path element
            # link_url.query => Query component
            try:
                print(urllib.parse.unquote(link_url.geturl())) # This reassembles the parts of the url to the whole thing
            except:
                pass

# How many urls did we get on all pages?
print(sum(len(page['results']) for page in results))

# How many hits has google found with our keyword (as shown on the first page)?
print(results[0]['num_results_for_kw'])

[Warning: This description is deprecated, the sources are fresh tough]

How it works: Basically you create a GoogleScraper object with the query to search. You also specify the number of results per page (10,25,50 or 100) you want to obtain. Then you call the method search() on the new made object with a parameter indicating the number of pages too search for. I didn't try how many pages I can scrape\^\^ It's up to you to tell me!

The GoogleScraper.search() methods returns a list of special tuples! Each of these tuples represents a URL. If you want to know more about this special tupel, please read the python documentation.

The module is here:

  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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
Complete rewrite.
Many thanks go to v3nz3n

This is a little module that uses Google to automate search
queries. It gives straightforward access to all relevant data of Google such as
- The links of the result page
- The title of the links
- The caption/description below each link
- The number of results for this keyword

GoogleScraper's architecture outlined:
- Proxy support (Socks5, Socks4, HTTP Proxy)
- Threading support

The module implements some countermeasures to circumvent spamming detection
from the Google Servers:
{List them here}

Note: Scraping compromises the google terms of service (TOS).
"""

__VERSION__ = '0.4'
__UPDATED__ = '17.02.2014' # day.month.year
__AUTHOR__ = 'Nikolai Tschacher'
__WEBSITE__ = 'incolumitas.com'

import sys
import os
import socket
import logging
import argparse
import threading
from collections import namedtuple
import hashlib
import re
import time
import lxml.html
import urllib.parse
from random import choice

try:
    import requests
    from cssselect import HTMLTranslator, SelectorError
    from bs4 import UnicodeDammit
    import socks # should be in the same directory
except ImportError as e:
    print(e.msg)
    print('You can install missing modules with `pip install [modulename]`')
    sys.exit(1)

# module wide global variables and configuration

# First obtain a logger
logger = logging.getLogger('GoogleScraper')
logger.setLevel(logging.INFO)

ch = logging.StreamHandler(stream=sys.stderr)
ch.setLevel(logging.INFO)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)

# Whether caching shall be enabled
DO_CACHING = True
# The directory path for cached google results
CACHEDIR = '.scrapecache/'

if DO_CACHING:
    if not os.path.exists(CACHEDIR):
        os.mkdir(CACHEDIR, 0o744)


class GoogleSearchError(Exception):
    def __init__(self):
        pass

    def __str__(self):
        return 'Exception in GoogleSearch class'


class InvalidNumberResultsException(GoogleSearchError):
    def __init__(self, number_of_results):
        self.nres = number_of_results

    def __str__(self):
        return '{} is not a valid number of results per page'.format(self.nres)


def maybe_clean_cache():
    """Delete all .cache files in the cache directory that are older than 12 hours."""
    for fname in os.listdir(CACHEDIR):
        if time.time() > os.path.getmtime(os.path.join(CACHEDIR, fname)) + (60 * 60 * 12):
            os.remove(os.path.join(CACHEDIR, fname))

if DO_CACHING:
    # Clean the CACHEDIR once in a while
    maybe_clean_cache()

def cached_file_name(search_params):
    sha = hashlib.sha256()
    # Make a unique file name based on the values of the google search parameters.
    sha.update(b''.join(str(search_params.get(s)).encode() for s in sorted(search_params.keys())))
    return '{}.{}'.format(sha.hexdigest(), 'cache')


def get_cached(search_params):
    """Loads a cached search results page from scrapecache/fname.cache

    It helps in testing and avoid requesting
    the same resources again and again (such that google may
    recognize us as what we are: Sneaky SEO crawlers!)
    """
    fname = cached_file_name(search_params)

    try:
        if fname in os.listdir(CACHEDIR):
            # If the cached file is older than 12 hours, return False and thus
            # make a new fresh request.
            modtime = os.path.getmtime(os.path.join(CACHEDIR, fname))
            if (time.time() - modtime) / 60 / 60 > 12:
                return False
            with open(os.path.join(CACHEDIR, fname), 'r') as fd:
                return fd.read()
    except FileNotFoundError as err:
        raise Exception('Unexpected file not found: {}'.format(err.msg))

    return False


def cache_results(search_params, html):
    """Stores a html resource as a file in scrapecache/fname.cache

    This will always write(overwrite) the cache file.
    """
    fname = cached_file_name(search_params)

    with open(os.path.join(CACHEDIR, fname), 'w') as fd:
        fd.write(html)


class GoogleScrape(threading.Thread):
    """Offers a fast way to query the google search engine.

    Overrides the run() method of the superclass threading.Thread.
    Each thread represents a crawl for one Google Results Page.

    http://www.blueglass.com/blog/google-search-url-parameters-query-string-anatomy/
    """

    # Valid URL (taken from django)
    _REGEX_VALID_URL = re.compile(
        r'^(?:http|ftp)s?://' # http:// or https://
        r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
        r'localhost|' # localhost...
        r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
        r'(?::\d+)?' # optional port
        r'(?:/?|[/?]\S+)$', re.IGNORECASE)
    _REGEX_VALID_URL_SIMPLE = re.compile(
        'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')

    # Named tuple type for the search results
    Result = namedtuple('LinkResult', 'link_title link_snippet link_url')

    # Several different User-Agents to diversify the requests.
    # Keep the User-Agents updated. Last update: 17th february 14
    # Get them here: http://techblog.willshouse.com/2012/01/03/most-common-user-agents/
    _UAS = [
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.73.11 (KHTML, like Gecko) Version/7.0.1 Safari/537.73.11',
        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36',
        'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0',
        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36',
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36',
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36',
        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36',
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36',
        'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B554a Safari/9537.53',
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:26.0) Gecko/20100101 Firefox/26.0',
        'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0',
        'Mozilla/5.0 (iPad; CPU OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B554a Safari/9537.53',
        'Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0',
        'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36'
    ]

    _HEADERS = {
        'User-Agent': 'Mozilla/5.0',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Connection': 'close',
        'DNT': '1'
    }

    def __init__(self, search_query, num_results_per_page=10, num_page=0, search_params={}):
        """Initialises an object responsible for scraping one particular results page.

        @param search_query: The query to scrape for.
        @param num_results_per_page: The number of results per page. Must be smaller than 1000.
        (My tests though have shown that at most 100 results were returned per page)
        @param num_page: The number/index of the page.
        @param search_params: A dictionary with additional search params. The default search params is updated with this parameter.
        """
        super().__init__()
        logger.debug("Created new GoogleScrape object with params: query={}, num_results_per_page={}, num_page={}".format(
            search_query, num_results_per_page, num_page))
        self.search_query = search_query
        if num_results_per_page not in range(0, 1001): # The maximum value of this parameter is 1000. See search appliance docs
            logger.error('The parameter -n must be smaller or equal to 1000')
            raise InvalidNumberResultsException(num_results_per_page)

        if num_page*num_results_per_page + num_results_per_page > 1000:
            logger.error('The maximal number of results for a query is 1000')
            raise InvalidNumberResultsException(num_page*num_results_per_page + num_results_per_page)

        self.num_results_per_page = num_results_per_page
        self.num_page = num_page

        self._SEARCH_URL = 'http://www.google.com/search'

        # http://www.rankpanel.com/blog/google-search-parameters/
        self._SEARCH_PARAMS = {
            'q': '', # the search query string
            'num': '', # the number of results per page
            'numgm': None, # Number of KeyMatch results to return with the results. A value between 0 to 50 can be specified for this option.
            'start': '0', # Specifies the index number of the first entry in the result set that is to be returned. page number = (start / num) + 1
                          # The maximum number of results available for a query is 1,000, i.e., the value of the start parameter added to the value of the num parameter cannot exceed 1,000.
            'rc': '', # Request an accurate result count for up to 1M documents. If a user submits a search query without the site parameter, the entire search index is queried.
            'site': None, # Limits search results to the contents of the specified collection.
            'sort': None, # Specifies a sorting method. Results can be sorted by date.
            'client': None, # required parameter. Indicates a valid front end.
            'output': None, # required parameter. Selects the format of the search results.
            'partialfields': None, # Restricts the search results to documents with meta tags whose values contain the specified words or phrases.
            'pws': '0',      # personalization turned off
            'cd': None, # Passes down the keyword rank clicked.
            'filter': 0, # Include omitted results
            'complete': 0, #Turn auto-suggest and Google Instant on (=1) or off (=0)
            'nfpr': 1, #Turn off auto-correction of spelling
            'ncr': 1, #No country redirect: Allows you to set the Google country engine you would like to use despite your current geographic location.
            'safe': 'off', # Turns the adult content filter on or off
            'rls': None, #Source of query with version of the client and language set, other examples are can be found
            'source': None,  #Google navigational parameter specifying where you came from, here universal search
            'tbm': None, # Used when you select any of the “special” searches, like image search or video search
            'tbs': None, # Also undocumented as `tbm`, allows you to specialize the time frame of the results you want to obtain.
                         # Examples: Any time: tbs=qdr:a, Last second: tbs=qdr:s, Last minute: tbs=qdr:n, Last day: tbs=qdr:d, Time range: tbs=cdr:1,cd_min:3/2/1984,cd_max:6/5/1987
                         # But the tbs parameter is also used to specify content:
                         # Examples: Sites with images: tbs=img:1, Results by reading level, Basic level: tbs=rl:1,rls:0, Results that are translated from another language: tbs=clir:1,
                         # For full documentation, see http://stenevang.wordpress.com/2013/02/22/google-search-url-request-parameters/
            'lr': 'lang_de', # Restricts searches to pages in the specified language. If there are no results in the specified language, the search appliance displays results in all languages .
                             # lang_xx where xx is the country code such as en, de, fr, ca, ...
            'hl': 'en', # Language settings passed down by your browser
            'cr': 'countryDE', # The region the results should come from
            'gr': None, # Just as gl shows you how results look in a specified country, gr limits the results to a certain region
            'gcs': None, # Limits results to a certain city, you can also use latitude and longitude
            'gpc': None, #Limits results to a certain zip code
            'gm': None, # Limits results to a certain metropolitan region
            'gl': 'de', # as if the search was conducted in a specified location. Can be unreliable.
            'ie': 'utf-8', # Sets the character encoding that is used to interpret the query string.
            'oe': 'utf-8' # Sets the character encoding that is used to encode the results.
        }

        # Maybe update the default search params when the user has supplied a dictionary
        if search_params is not None and isinstance(search_params, dict):
            self._SEARCH_PARAMS.update(search_params)

        self.SEARCH_RESULTS = {
            'cache_file': None, # A path to a file that caches the results.
            'search_keyword': self.search_query, # The query keyword
            'num_results_for_kw': '', # The number of results for the keyword
            'results': [] # List of Result named tuples
        }

    def run(self):
        """Make the the scrape and clean the URL's."""
        self._search()

        # Now try to create ParseResult objects from the URL
        for i, e in enumerate(self.SEARCH_RESULTS['results']):
            try:
                url = re.search(r'/url\?q=(?P.*?)&sa=U&ei=', e.link_url).group(1)
                assert self._REGEX_VALID_URL.match(url).group()
                self.SEARCH_RESULTS['results'][i] =   
                   self.Result(link_title=e.link_title, link_url=urllib.parse.urlparse(url),
                                link_snippet=e.link_snippet)
            except Exception as err:
                logger.warn("URL={} found to be invalid.".format(url))

    def _build_query(self, random=False):
        """Build the headers and params for the GET request towards the Google server.

        When random is True, several headers (like the UA) are chosen
        randomly.
        """
        self._SEARCH_PARAMS.update(
            {'q': self.search_query,
             'num': str(self.num_results_per_page),
             'start': str(int(self.num_results_per_page) * int(self.num_page))
            })

        if random:
            self._HEADERS['User-Agent'] = choice(self._UAS)

    def _search(self):
        """The actual search and parsing of the results.

        Private, internal method.
        Parsing is done with lxml and cssselect. The html structure of the Google Search
        results may change over time. Effective: February 2014
        """
        self._build_query()

        if DO_CACHING:
            html = get_cached(self._SEARCH_PARAMS)
            self.SEARCH_RESULTS['cache_file'] = os.path.join(CACHEDIR, cached_file_name(self._SEARCH_PARAMS))
        else:
            html = False

        if not html:
            try:
                r = requests.get(self._SEARCH_URL, headers=self._HEADERS,
                                 params=self._SEARCH_PARAMS, timeout=3.0)

                logger.debug("Scraped with url: {}".format(r.url))

            except requests.ConnectionError as cerr:
                print('Network problem occurred {}'.format(cerr.msg))
                return False
            except requests.Timeout as terr:
                print('Connection timeout {}'.format(terr.msg))
                return False

            if not r.ok:
                print('HTTP Error:', r.status_code)
                if str(r.status_code)[0] == '5':
                    print('Maybe google recognizes you as sneaky spammer after'
                          ' you requested their services too inexhaustibly :D')
                return False

            html = r.text
            # cache fresh results
            if DO_CACHING:
                cache_results(self._SEARCH_PARAMS, html)
                self.SEARCH_RESULTS['cache_file'] = os.path.join(CACHEDIR, cached_file_name(self._SEARCH_PARAMS))

        # Try to parse the google HTML result using lxml
        try:
            doc = UnicodeDammit(html, is_html=True)
            parser = lxml.html.HTMLParser(encoding=doc.declared_html_encoding)
            dom = lxml.html.document_fromstring(html, parser=parser)
            dom.resolve_base_href()
        except Exception as e:
            print('Some error occurred while lxml tried to parse: {}'.format(e.msg))
            return False

        # Try to extract all links of non-ad results, including their snippets(descriptions) and titles.
        try:
            li_g_results = dom.xpath(HTMLTranslator().css_to_xpath('li.g'))
            links = []
            for e in li_g_results:
                try:
                    link_element = e.xpath(HTMLTranslator().css_to_xpath('h3.r > a:first-child'))
                    link = link_element[0].get('href')
                    title = link_element[0].text_content()
                except IndexError as err:
                    logger.error('Error while parsing link/title element: {}'.format(err))
                    continue
                try:
                    snippet_element = e.xpath(HTMLTranslator().css_to_xpath('div.s > span.st'))
                    snippet = snippet_element[0].text_content()
                except IndexError as err:
                    logger.error('Error while parsing snippet element: {}'.format(err))
                    continue

                links.append(self.Result(link_title=title, link_url=link, link_snippet=snippet))
        # Catch further errors besides parsing errors that take shape as IndexErrors
        except Exception as err:
            logger.error('Error in parsing result links: {}'.format(err))

        self.SEARCH_RESULTS['results'].extend(links)

        # try to get the number of results for our search query
        try:
            self.SEARCH_RESULTS['num_results_for_kw'] =   
               dom.xpath(HTMLTranslator().css_to_xpath('div#resultStats'))[0].text_content()
        except Exception as e:
            logger.critical(e.msg)


def scrape(query, num_results_per_page=100, num_pages=1, offset=0):
    """Public API function to search for terms and return a list of results.

    arguments:
    query -- the search query. Can be whatever you want to crawl google for.

    Keyword arguments:
    num_results_per_page -- the number of results per page. Either 10, 25, 50 or 100.
    num_pages -- The number of pages to search for.
    offset -- specifies the offset to the page to begin searching.

    """
    threads = [GoogleScrape(query, num_results_per_page, i) for i in range(offset, num_pages + offset, 1)]

    for t in threads:
        t.start()

    for t in threads:
        t.join(3.0)

    return [t.SEARCH_RESULTS for t in threads]


def deep_scrape(query):
    """Launches many different Google searches with different parameter combinations to maximize return of results.

    @param query: The query to search for.
    @return: All the result sets.
    """

    # First obtain some synonyms for the search query

    # For each proxy, run the scrapes


if __name__ == '__main__':
    parser = argparse.ArgumentParser(prog='GoogleScraper', description='Scrapes the Google search engine',
                                     epilog='This program might infringe Google TOS, so use at your own risk')
    parser.add_argument('-q', '--query', metavar='search_string', type=str, action='store', dest='query', required=True,
                        help='The search query.')
    parser.add_argument('-n', '--num_results_per_page', metavar='number_of_results_per_page', type=int,
                        dest='num_results_per_page', action='store', default=50,
                        help='The number of results per page. Most be >= 100')
    parser.add_argument('-p', '--num_pages', metavar='num_of_pages', type=int, dest='num_pages', action='store',
                        default=1,
                        help='The number of pages to search in. Each page is requested by a unique connection and if possible by a unique IP.')
    parser.add_argument('--proxy', metavar='proxycredentials', type=str, dest='proxy', action='store',
                        required=False, #default=('127.0.0.1', 9050)
                        help='A string such as "127.0.0.1:9050" specifying a single proxy server')
    parser.add_argument('--proxy_file', metavar='proxyfile', type=str, dest='proxy_file', action='store',
                        required=False, #default='.proxies'
                        help='A filename for a list of proxies (supported are HTTP PROXIES, SOCKS4/4a/5) with the following format: "Proxyprotocol (proxy_ip|proxy_host):Port\\n"')
    parser.add_argument('-x', '--deep-scrape', action='store_true', default=False, help='Launches a wide range of parallel searches by modifying the search '   
                       'query string with synonyms and by scraping with different Google search parameter combinations that might yield more unique '   
                       'results. The algorithm is optimized for maximum of results for a specific keyword whilst trying avoid detection. This is the heart of GoogleScraper.')
    parser.add_argument('--view', action='store_true', default=False, help="View the response in a default browser tab."
                                                                 " Mainly for debug purposes. Works only when caching is enabled.")
    parser.add_argument('-v', '--verbosity', type=int, default=1,
                        help="The verbosity of the output reporting for the found search results.")
    args = parser.parse_args()

    if args.proxy_file:
        raise NotImplementedError('Coming soon.')

    if args.proxy:
        def create_connection(address, timeout=None, source_address=None):
            sock = socks.socksocket()
            sock.connect(address)
            return sock

        proxy_host, proxy_port = args.proxy.split(':')

        # Patch the socket module
        socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, proxy_host, int(proxy_port),
                              rdns=True) # rdns is by default on true. Never use rnds=False with TOR, otherwise you are screwed!
        socks.wrap_module(socket)
        socket.create_connection = create_connection

    if args.deep_scrape:
        results = deep_scrape(args.query)
    else:
        results = scrape(args.query, args.num_results_per_page, args.num_pages)

    for result in results:
        logger.info('{} links found! The search with the keyword "{}" yielded the result:{}'.format(
            len(result['results']), result['search_keyword'], result['num_results_for_kw']))
        if args.view:
            import webbrowser
            webbrowser.open(result['cache_file'])
        for link_title, link_snippet, link_url in result['results']:
            print('Link: {}'.format(urllib.parse.unquote(link_url.geturl())))
            if args.verbosity > 1:
                import textwrap
                print('Title: \n{}'.format(textwrap.indent('\n'.join(textwrap.wrap(link_title, 50)), '\t')))
                print(
                    'Description: \n{}\n'.format(textwrap.indent('\n'.join(textwrap.wrap(link_snippet, 70)), '\t')))
                print('*' * 70)
   print('*' * 70)