Skip to content

Commit eccdd01

Browse files
author
Daniel Dabrowski
committed
get google trents cookies on init and use them on all requests to get results
1 parent a67cc31 commit eccdd01

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

pytrends/request.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ def __init__(self, hl='en-US', tz=360, geo='', proxies=''):
4444
self.hl = hl
4545
self.geo = geo
4646
self.kw_list = list()
47-
self.proxies = proxies #add a proxy option
47+
self.proxies = proxies #add a proxy option
4848
#proxies format: {"http": "http://192.168.0.1:8888" , "https": "https://192.168.0.1:8888"}
49+
self.cookies = requests.get('https://trends.google.com').cookies
4950

5051
# intialize widget payloads
5152
self.token_payload = dict()
@@ -64,16 +65,13 @@ def _get_data(self, url, method=GET_METHOD, trim_chars=0, **kwargs):
6465
:param kwargs: any extra key arguments passed to the request builder (usually query parameters or data)
6566
:return:
6667
"""
68+
s = requests.session()
69+
if self.proxies != '':
70+
s.proxies.update(self.proxies)
6771
if method == TrendReq.POST_METHOD:
68-
s = requests.session()
69-
if self.proxies != '':
70-
s.proxies.update(self.proxies)
71-
response = s.post(url, **kwargs)
72+
response = s.post(url, cookies=self.cookies, **kwargs)
7273
else:
73-
s = requests.session()
74-
if self.proxies != '':
75-
s.proxies.update(self.proxies)
76-
response = s.get(url,**kwargs)
74+
response = s.get(url, cookies=self.cookies, **kwargs)
7775

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

pytrends/test_trendReq.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
from requests.cookies import RequestsCookieJar
12
from unittest import TestCase
3+
24
from pytrends.request import TrendReq
35

46

@@ -10,6 +12,7 @@ def test__get_data(self):
1012
self.assertEqual(pytrend.hl, 'en-US')
1113
self.assertEqual(pytrend.tz, 360)
1214
self.assertEqual(pytrend.geo, '')
15+
self.assertIsInstance(pytrend.cookies, RequestsCookieJar)
1316

1417
def test_build_payload(self):
1518
"""Should return the widgets to get data"""

0 commit comments

Comments
 (0)