From 525294f9b2eef3cbfc098f2b5d8db09a8641bd7f Mon Sep 17 00:00:00 2001 From: M4rkoHR Date: Sat, 6 Feb 2021 19:51:32 +0100 Subject: [PATCH 1/2] Add SafeSearch support --- README.md | 7 +++++-- gsearch/googlesearch.py | 8 ++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b0bfbd5..01a42a3 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ But this turned out to be pretty generic, feel free to use it for your own work. * Free unrestricted API, requires no key or credit card * Unicode support * Works for all Python versions (2 & 3) +* SafeSearch support ### Installation @@ -36,12 +37,14 @@ pip install gsearch ```sh > from gsearch.googlesearch import search -> results = search('Full Stack Developer') # returns 10 or less results +> results = search('Full Stack Developer') # returns 10 or less results with Safe Search OFF [ ('Name', 'Link'), ('Name', 'Link'), ... ] -> results = search('Avi Aryan', num_results=20) # returns 20 or less results +> results = search('Avi Aryan', num_results=20) # returns 20 or less results with Safe Search OFF + +> results = search('Rimac', safe_search=True) # returns 10 or less results with Safe Search ON ``` You can also use it as a CLI tool. diff --git a/gsearch/googlesearch.py b/gsearch/googlesearch.py index c60673f..88a218a 100644 --- a/gsearch/googlesearch.py +++ b/gsearch/googlesearch.py @@ -33,7 +33,7 @@ isPython2 = sys.version.startswith('2') -def download(query, num_results): +def download(query, num_results, safe_search=False): """ downloads HTML after google search """ @@ -41,7 +41,7 @@ def download(query, num_results): name = quote(query) name = name.replace(' ','+') - url = 'http://www.google.com/search?q=' + name + url = 'http://www.google.com/search?q=' + name + ("&safe=active" if safe_search else "&safe=images") if num_results != 10: url += '&num=' + str(num_results) # adding this param might hint Google towards a bot req = request.Request(url, headers={ @@ -98,12 +98,12 @@ def convert_unicode(text): return s -def search(query, num_results=10): +def search(query, num_results=10, safe_search=False): """ searches google for :query and returns a list of tuples of the format (name, url) """ - data = download(query, num_results) + data = download(query, num_results, safe_search) results = re.findall(r'\.*?\<\/h3\>', data, re.IGNORECASE) if results is None or len(results) == 0: print('No results where found. Did the rate limit exceed?') From f35c8348a99cb41f1ef1a527f3ebd7c04b548ac1 Mon Sep 17 00:00:00 2001 From: M4rkoHR Date: Sat, 6 Feb 2021 19:55:19 +0100 Subject: [PATCH 2/2] SafeSearch update --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index e402c1f..6a98f54 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ def read(fname): install_requires = [], name = "gsearch", - version = "1.6.0", + version = "1.6.1", author = "Avi Aryan", author_email = "avi.aryan123@gmail.com", description = "Google Search unofficial API for Python with no external dependencies",