【问题标题】:error when connecting pytrends in Jupyter lab在 Jupyter 实验室中连接 pytrends 时出错
【发布时间】:2019-01-07 22:29:18
【问题描述】:

我正在使用:

  • Python 3.6.6
  • Mac OS High Sierra 10.13.6

和以下网站帮助我安装 pytrends https://pypi.org/project/pytrends/

我按照说明下载 pytrends 并安装了运行 pytrends "requestslxmlpandas" 的要求。以下是说明

  1. 安装 pytrends pip install pytrends
  2. pytrends 连接到 google

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

但我收到以下错误

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

所以我研究了一些信息来帮助我,并从https://github.com/GeneralMills/pytrends/blob/master/README.md找到了一个更适合我的代码

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

但我收到以下错误

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'

我在 Jupiter 实验室运行了上述代码。我的猜测是我必须在 Jupiter 实验室中导入 pytrends。我安装了 pytrends,但通过终端而不是 Jupyter 实验室。我将在 Jupiter 实验室尝试!pip3 install pytrends。我通过阅读某人提出的问题得到了这个想法

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

除了上面的链接之外,我还发现了另外两个关于堆栈溢出的相关问题,这可能有助于我解决这个问题:

Jupyter Notebook: no module named pandas

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

【问题讨论】:

    标签: python jupyter-notebook importerror traceback


    【解决方案1】:

    从命令行安装 pyTrends 后,在 Jupyter Lab 中,您希望实例化一个新笔记本并运行以下代码,您将能够打印在 _timeframe 变量 I 中找到的时段的 Google 趋势数据已经声明了。

    kw_list中的搜索词更改为您要查找搜索趋势数据的词,如下所示:

    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))
    

    【讨论】:

      猜你喜欢
      • 2022-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-27
      相关资源
      最近更新 更多