From eccdd01c562c09adf3ef16078c3962928cc7ce1a Mon Sep 17 00:00:00 2001
From: Daniel Dabrowski
Date: Tue, 29 May 2018 16:47:54 +0200
Subject: [PATCH] get google trents cookies on init and use them on all
requests to get results
---
pytrends/request.py | 16 +++++++---------
pytrends/test_trendReq.py | 3 +++
2 files changed, 10 insertions(+), 9 deletions(-)
diff --git a/pytrends/request.py b/pytrends/request.py
index 1fbc3064..0bf1b404 100644
--- a/pytrends/request.py
+++ b/pytrends/request.py
@@ -44,8 +44,9 @@ def __init__(self, hl='en-US', tz=360, geo='', proxies=''):
self.hl = hl
self.geo = geo
self.kw_list = list()
- self.proxies = proxies #add a proxy option
+ self.proxies = proxies #add a proxy option
#proxies format: {"http": "http://192.168.0.1:8888" , "https": "https://192.168.0.1:8888"}
+ self.cookies = requests.get('https://trends.google.com').cookies
# intialize widget payloads
self.token_payload = dict()
@@ -64,16 +65,13 @@ def _get_data(self, url, method=GET_METHOD, trim_chars=0, **kwargs):
:param kwargs: any extra key arguments passed to the request builder (usually query parameters or data)
:return:
"""
+ s = requests.session()
+ if self.proxies != '':
+ s.proxies.update(self.proxies)
if method == TrendReq.POST_METHOD:
- s = requests.session()
- if self.proxies != '':
- s.proxies.update(self.proxies)
- response = s.post(url, **kwargs)
+ response = s.post(url, cookies=self.cookies, **kwargs)
else:
- s = requests.session()
- if self.proxies != '':
- s.proxies.update(self.proxies)
- response = s.get(url,**kwargs)
+ response = s.get(url, cookies=self.cookies, **kwargs)
# check if the response contains json and throw an exception otherwise
# Google mostly sends 'application/json' in the Content-Type header,
diff --git a/pytrends/test_trendReq.py b/pytrends/test_trendReq.py
index 5029519e..1f96c4d4 100644
--- a/pytrends/test_trendReq.py
+++ b/pytrends/test_trendReq.py
@@ -1,4 +1,6 @@
+from requests.cookies import RequestsCookieJar
from unittest import TestCase
+
from pytrends.request import TrendReq
@@ -10,6 +12,7 @@ def test__get_data(self):
self.assertEqual(pytrend.hl, 'en-US')
self.assertEqual(pytrend.tz, 360)
self.assertEqual(pytrend.geo, '')
+ self.assertIsInstance(pytrend.cookies, RequestsCookieJar)
def test_build_payload(self):
"""Should return the widgets to get data"""