Search code examples
pythoncsvipythonjupytergoogle-trends

Google Trends "An error has been detected"


I have the following code which I used a code sample from dominolab:

from pytrends.pyGTrends import pyGTrends
import time
from random import randint
from IPython.display import display
from pprint import pprint
import urllib
import sys
import os

google_username = "myusername"
google_password = "mypassword"
path = "csv_files"

if not os.path.exists(path):
    os.makedirs(path)

base_keyword = "/m/0k44x" #Image Processing

terms = [
    "Image Processing",
    "Signal Processing",
    "Computer Vision",
    "Machine Learning",
    "Information Retrieval",
    "Data Mining"
]

advanced_terms = [
    "/m/07844",
    "/m/0yk6",
    "/m/05kx1v",
    "/m/04zv0zl",
    "/m/017chx",
    "/m/0cqyr9",
    "/m/0121sb",
    "/m/07844",
    "/m/06dq9"
]
# connect to Google Trends API
connector = pyGTrends(google_username, google_password)


for label, keyword in zip(terms, advanced_terms):
    print(label)
    sys.stdout.flush()
    keyword_string = '"{0}, {1}"'.format(keyword, base_keyword)
    connector.request_report(keyword_string, geo="US", date="01/2014 65m")
    # wait a random amount of time between requests to avoid bot detection
    time.sleep(randint(5, 10))
    # download file
    connector.save_csv(path, label)

for term in terms:
    data = connector.get_suggestions(term)
    pprint(data)

However, I see these in the saved CSV files:

<div id="report">
    <div class="errorTitle">An error has been detected</div>
    <div class="errorSubTitle">This page is currently unavailable. Please try again later.<br/> Please make sure your query is valid and try again.<br/> If you're experiencing long delays, consider reducing your comparison items.<br/> Thanks for your patience.</div>
  </div>

What has gone wrong and how can this be fixed? I get the data from Google Trend and here's an example.


Solution

  • I think the problem is the date query, date = "01/2014 65m", you're asking it for 65 months after January 2014 ... but that's in the future!

    Hence this is the correct format for date:

    connector.request_report(keyword_string, geo="US", date="01/2014 5m")