Repository: GeneralMills/pytrends

Issue:
Pytrends error 400
Hi all, 

I am trying to pull some data from Google Trends but I keep getting the error "The request failed: Google returned a response with code 400.". I think it might has something to do with pytrend.interest_over_time() but I haven't been able to fix it. 

import csv
import time
import pandas as pd
from pytrends.request import TrendReq


pytrend = TrendReq(hl='DK', tz=360)
keywords =['Scandlines']
pytrend.build_payload(
    kw_list=keywords,
    cat=0,
    timeframe='2016-12-14 2017-01-25',
    geo='DK',
    gprop=' '
)


data = pytrend.interest_over_time()
data.to_csv('Uge 16', encoding='utf_8_sig')

Anybody having similar issues?


Comments:

Patch:
diff --git a/README.md b/README.md
index d348f16..3d4d350 100644
--- a/README.md
+++ b/README.md
@@ -39,7 +39,7 @@ Only good until Google changes their backend again :-P. When that happens feel f
 
 ## Requirements
 
-* Written for both Python 2.7+ and Python 3.3+
+* Written for Python 3.3+
 * Requires Requests, lxml, Pandas
 
 <sub><sup>[back to top](#pytrends)</sub></sup>
diff --git a/pytrends/request.py b/pytrends/request.py
index e2ae29d..8e7d48c 100644
--- a/pytrends/request.py
+++ b/pytrends/request.py
@@ -1,5 +1,3 @@
-from __future__ import absolute_import, print_function, unicode_literals
-
 import json
 import sys
 import time
@@ -14,10 +12,7 @@ from requests.packages.urllib3.util.retry import Retry
 
 from pytrends import exceptions
 
-if sys.version_info[0] == 2:  # Python 2
-    from urllib import quote
-else:  # Python 3
-    from urllib.parse import quote
+from urllib.parse import quote
 
 
 class TrendReq(object):
@@ -150,6 +145,8 @@ class TrendReq(object):
     def build_payload(self, kw_list, cat=0, timeframe='today 5-y', geo='',
                       gprop=''):
         """Create the payload for related queries, interest over time and interest by region"""
+        if gprop not in ['', 'images', 'news', 'youtube', 'froogle']:
+            raise ValueError('gprop must be empty (to indicate web), images, news, youtube, or froogle')
         self.kw_list = kw_list
         self.geo = geo or self.geo
         self.token_payload = {
@@ -241,6 +238,8 @@ class TrendReq(object):
             result_df2 = df['isPartial'].apply(lambda x: pd.Series(
                 str(x).replace('[', '').replace(']', '').split(',')))
             result_df2.columns = ['isPartial']
+            # Change to a bool type.
+            result_df2.isPartial = result_df2.isPartial == 'True'
             # concatenate the two dataframes
             final = pd.concat([result_df, result_df2], axis=1)
         else:
@@ -285,7 +284,7 @@ class TrendReq(object):
         # rename the column with the search keyword
         df = df[['geoName', 'geoCode', 'value']].set_index(
             ['geoName']).sort_index()
-        # split list columns into seperate ones, remove brackets and split on comma
+        # split list columns into separate ones, remove brackets and split on comma
         result_df = df['value'].apply(lambda x: pd.Series(
             str(x).replace('[', '').replace(']', '').split(',')))
         if inc_geo_code:
@@ -398,7 +397,7 @@ class TrendReq(object):
         """Request data from Google's Hot Searches section and return a dataframe"""
 
         # make the request
-        # forms become obsolute due to the new TRENDING_SEACHES_URL
+        # forms become obsolete due to the new TRENDING_SEARCHES_URL
         # forms = {'ajax': 1, 'pn': pn, 'htd': '', 'htv': 'l'}
         req_json = self._get_data(
             url=TrendReq.TRENDING_SEARCHES_URL,
@@ -479,7 +478,7 @@ class TrendReq(object):
                                 geo='', gprop='', sleep=0):
         """Gets historical hourly data for interest by chunking requests to 1 week at a time (which is what Google allows)"""
 
-        # construct datetime obejcts - raises ValueError if invalid parameters
+        # construct datetime objects - raises ValueError if invalid parameters
         initial_start_date = start_date = datetime(year_start, month_start,
                                                    day_start, hour_start)
         end_date = datetime(year_end, month_end, day_end, hour_end)
diff --git a/setup.py b/setup.py
index 717fc7a..8ced39d 100644
--- a/setup.py
+++ b/setup.py
@@ -21,7 +21,6 @@ setup(
     classifiers=[
         'Development Status :: 4 - Beta',
         'Intended Audience :: Developers',
-        'Programming Language :: Python :: 2.7',
         'Programming Language :: Python :: 3.3',
         'Programming Language :: Python :: 3.4',
         'Programming Language :: Python :: 3.5',


Test patch:
diff --git a/pytrends/test_trendReq.py b/pytrends/test_trendReq.py
index 80a4748..8c3efd3 100644
--- a/pytrends/test_trendReq.py
+++ b/pytrends/test_trendReq.py
@@ -1,4 +1,5 @@
 from unittest import TestCase
+import pandas.api.types as ptypes
 
 from pytrends.request import TrendReq
 
@@ -29,6 +30,31 @@ class TestTrendReq(TestCase):
         pytrend.build_payload(kw_list=['pizza', 'bagel'])
         self.assertIsNotNone(pytrend.interest_over_time())
 
+    def test_interest_over_time_images(self):
+        pytrend = TrendReq()
+        pytrend.build_payload(kw_list=['pizza', 'bagel'], gprop='images')
+        self.assertIsNotNone(pytrend.interest_over_time())
+
+    def test_interest_over_time_news(self):
+        pytrend = TrendReq()
+        pytrend.build_payload(kw_list=['pizza', 'bagel'], gprop='news')
+        self.assertIsNotNone(pytrend.interest_over_time())
+
+    def test_interest_over_time_youtube(self):
+        pytrend = TrendReq()
+        pytrend.build_payload(kw_list=['pizza', 'bagel'], gprop='youtube')
+        self.assertIsNotNone(pytrend.interest_over_time())
+
+    def test_interest_over_time_froogle(self):
+        pytrend = TrendReq()
+        pytrend.build_payload(kw_list=['pizza', 'bagel'], gprop='froogle')
+        self.assertIsNotNone(pytrend.interest_over_time())
+
+    def test_interest_over_time_bad_gprop(self):
+        pytrend = TrendReq()
+        with self.assertRaises(ValueError):
+            pytrend.build_payload(kw_list=['pizza', 'bagel'], gprop=' ')
+
     def test_interest_by_region(self):
         pytrend = TrendReq()
         pytrend.build_payload(kw_list=['pizza', 'bagel'])
@@ -47,14 +73,27 @@ class TestTrendReq(TestCase):
     def test_trending_searches(self):
         pytrend = TrendReq()
         pytrend.build_payload(kw_list=['pizza', 'bagel'])
-        self.assertIsNotNone(pytrend.trending_searches(pn='p1'))
+        self.assertIsNotNone(pytrend.trending_searches())
 
     def test_top_charts(self):
         pytrend = TrendReq()
         pytrend.build_payload(kw_list=['pizza', 'bagel'])
-        self.assertIsNotNone(pytrend.top_charts(cid='actors', date=201611))
+        self.assertIsNotNone(pytrend.top_charts(date=2019))
 
     def test_suggestions(self):
         pytrend = TrendReq()
         pytrend.build_payload(kw_list=['pizza', 'bagel'])
         self.assertIsNotNone(pytrend.suggestions(keyword='pizza'))
+
+    def test_ispartial_dtype(self):
+        pytrend = TrendReq()
+        pytrend.build_payload(kw_list=['pizza', 'bagel'])
+        df = pytrend.interest_over_time()
+        assert ptypes.is_bool_dtype(df.isPartial)
+
+    def test_ispartial_dtype_timeframe_all(self):
+        pytrend = TrendReq()
+        pytrend.build_payload(kw_list=['pizza', 'bagel'],
+                              timeframe='all')
+        df = pytrend.interest_over_time()
+        assert ptypes.is_bool_dtype(df.isPartial)