Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get google trents cookies on init and use them on all requests to get results #244

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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