Search code examples
pythonjupyter-notebookimporterrortraceback

error when connecting pytrends in Jupyter lab


I am using :

  • Python 3.6.6
  • Mac OS High Sierra 10.13.6

and the following website to help me install pytrends https://pypi.org/project/pytrends/

I am following the instructions to download pytrends and installed the requirements to run pytrends "requests, lxml, & pandas". Here are the instructions

  1. Install pytrends pip install pytrends
  2. Connect pytrends to google

from pytrends.request import TrendReq pytrends = TrendReq(hl=’en-US’, tz=360)

but I get the following error

File "<ipython-input-1-e31d93dc256d>", line 2
pytrends = TrendReq(hl=’en-US’, tz=360)
                         ^ SyntaxError: invalid character in identifier

so I researched for information to help me and found a code that worked better for me from https://github.com/GeneralMills/pytrends/blob/master/README.md

from pytrends.request import TrendReqpytrends = TrendReq(hl='en-US', tz=360)

but I received the following error

ModuleNotFoundError Traceback (most recent call last) <ipython-input-9d1eaf7e6778a>in <module>() ----> 
  1 from pytrends.request import TrendReq
  2 
  3 pytrends = TrendReq(hl='en-US', tz=360) ModuleNotFoundError: No module named 'pytrends'

I ran the above mentioned code in Jupiter labs. My guess is that I have to import pytrends in Jupiter lab. I installed pytrends but through terminal not Jupyter labs. I will try !pip3 install pytrends in Jupiter lab. I got this idea by reading an issue someone has from

https://github.com/GeneralMills/pytrends/issues/248

In addition to link above, I found two other relevant questions on stack overflow that might help me solve this issue :

Jupyter Notebook: no module named pandas

numpy & pandas 'ModuleNotFoundEror' in Jupyter notebook (Python 3)


Solution

  • After you install pyTrends from the command line, inside of Jupyter Lab you want to instantiate a new note book and run the following code and you will be able to print Google Trends data for the period found in the _timeframe variable I have declared.

    Change the search term in kw_list to the term that you are looking for search trend data on as follows:

    from pytrends.request import TrendReq
    from matplotlib import pyplot as plt
    import numpy as np
    import pandas as pd
    
    # Create a Google Trend Object
    
    totalTrend = TrendReq(hl='en-US', tz=360)
    
    # Declare a var to store the search term
    #### build the playload
    kw_list = ["bitcoin"]  
    _cat = 0
    _geo = ''
    _gprop = ''
    
    # Build payload request to get data from Google trends
    _timeframe = '2009-01-03 2018-05-26'
    
    totalTrend.build_payload(kw_list, cat=_cat, timeframe=_timeframe, geo=_geo, gprop=_gprop)
    
    # Get interest over time
    # Capture Monthly Data for use in Normalization against Weekly
    totalTrend = totalTrend.interest_over_time()
    
    # Plot the Interest
    totalTrend.plot(title='Google Trends Monthly Data Points', figsize=(20,10))