Skip to content

Commit

Permalink
Merge pull request #244 from dabdada/get_session_cookie_for_trends_re…
Browse files Browse the repository at this point in the history
…quest

get google trents cookies on init and use them on all requests to get results
  • Loading branch information
dreyco676 authored May 29, 2018
2 parents a67cc31 + eccdd01 commit f653dc9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
16 changes: 7 additions & 9 deletions pytrends/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions pytrends/test_trendReq.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from requests.cookies import RequestsCookieJar
from unittest import TestCase

from pytrends.request import TrendReq


Expand All @@ -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"""
Expand Down

0 comments on commit f653dc9

Please sign in to comment.