From 08ab8b08e5bb9e823bbc093831e5306287c2c375 Mon Sep 17 00:00:00 2001 From: m_ashour Date: Mon, 19 Apr 2021 11:11:38 +0200 Subject: [PATCH 1/2] Added the support for proxies --- googlesearch/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/googlesearch/__init__.py b/googlesearch/__init__.py index 8368613..1497c09 100644 --- a/googlesearch/__init__.py +++ b/googlesearch/__init__.py @@ -1,8 +1,8 @@ -from requests import get from bs4 import BeautifulSoup +from requests import get -def search(term, num_results=10, lang="en"): +def search(term, num_results=10, lang="en", proxy=None): usr_agent = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/61.0.3163.100 Safari/537.36'} @@ -12,7 +12,8 @@ def fetch_results(search_term, number_results, language_code): google_url = 'https://www.google.com/search?q={}&num={}&hl={}'.format(escaped_search_term, number_results+1, language_code) - response = get(google_url, headers=usr_agent) + proxies = {"https":proxy} if proxy else None + response = get(google_url, headers=usr_agent, proxies=proxies) response.raise_for_status() return response.text From 2bc0efe41d0215533edfca78634a14eebb7414b6 Mon Sep 17 00:00:00 2001 From: m_ashour Date: Tue, 20 Apr 2021 12:08:54 +0200 Subject: [PATCH 2/2] Add dynamic for both http and https proxies --- googlesearch/__init__.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/googlesearch/__init__.py b/googlesearch/__init__.py index 1497c09..5ab1f14 100644 --- a/googlesearch/__init__.py +++ b/googlesearch/__init__.py @@ -12,8 +12,14 @@ def fetch_results(search_term, number_results, language_code): google_url = 'https://www.google.com/search?q={}&num={}&hl={}'.format(escaped_search_term, number_results+1, language_code) - proxies = {"https":proxy} if proxy else None - response = get(google_url, headers=usr_agent, proxies=proxies) + proxies = None + if proxy: + if proxy[:5]=="https": + proxies = {"https":proxy} + else: + proxies = {"http":proxy} + + response = get(google_url, headers=usr_agent, proxies=proxies) response.raise_for_status() return response.text