Skip to content

Commit 63cbadd

Browse files
authored
Update request.py
now supports proxy, #proxies format: {"http": "http://192.168.0.1:8888" , "https": "https://192.168.0.1:8888"}
1 parent 4fd7d84 commit 63cbadd

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

pytrends/request.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class TrendReq(object):
3131
SUGGESTIONS_URL = 'https://trends.google.com/trends/api/autocomplete/'
3232
CATEGORIES_URL = 'https://trends.google.com/trends/api/explore/pickers/category'
3333

34-
def __init__(self, hl='en-US', tz=360, geo=''):
34+
def __init__(self, hl='en-US', tz=360, geo='', proxies=''):
3535
"""
3636
Initialize default values for params
3737
"""
@@ -44,6 +44,8 @@ def __init__(self, hl='en-US', tz=360, geo=''):
4444
self.hl = hl
4545
self.geo = geo
4646
self.kw_list = list()
47+
self.proxies = proxies #add a proxy option
48+
#proxies format: {"http": "http://192.168.0.1:8888" , "https": "https://192.168.0.1:8888"}
4749

4850
# intialize widget payloads
4951
self.token_payload = dict()
@@ -63,9 +65,15 @@ def _get_data(self, url, method=GET_METHOD, trim_chars=0, **kwargs):
6365
:return:
6466
"""
6567
if method == TrendReq.POST_METHOD:
66-
response = requests.post(url, **kwargs)
68+
s = requests.session()
69+
if self.proxies != '':
70+
s.proxies.update(self.proxies)
71+
response = s.post(url, **kwargs)
6772
else:
68-
response = requests.get(url, **kwargs)
73+
s = requests.session()
74+
if self.proxies != '':
75+
s.proxies.update(self.proxies)
76+
response = s.get(url,**kwargs)
6977

7078
# check if the response contains json and throw an exception otherwise
7179
# Google mostly sends 'application/json' in the Content-Type header,

0 commit comments

Comments
 (0)