我在用 :


Python 3.6.6
Mac OS High Sierra 10.13.6


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

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


安装pytrends pip install pytrends
将pytrends连接到Google


from pytrends.request import TrendReqpytrends = 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)

最佳答案

从命令行安装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))

关于python - 在Jupyter实验室中连接pytrends时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51614853/

10-13 00:00