在Jupyter实验室中连接pytrends时出错

时间:2018-07-31 13:43:00

标签: python jupyter-notebook importerror traceback

我正在使用:

  • Python 3.6.6
  • Mac OS High Sierra 10.13.6

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

我正在按照说明下载pytrends,并安装了运行pytrends的要求“ 请求 lxml pandas ”。这是说明

  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实验室中运行了上述代码。我的猜测是我必须在木星实验室中导入pytrends。我安装了pytrends,但未通过终端安装Jupyter实验室。我将在木星实验室尝试!pip3 install pytrends。我是通过阅读某人的一个问题来得到这个主意的

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

除了上面的链接外,我还在堆栈溢出中发现了两个其他相关问题,它们可能可以帮助我解决此问题:

Jupyter Notebook: no module named pandas

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

1 个答案:

答案 0 :(得分:2)

从命令行安装pyTrends之后,您可以在Jupyter Lab内部实例化一个新的笔记本并运行以下代码,并且可以在_timeframe中找到的期间打印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))
相关问题