我正在使用:
- Python 3.6.6
- Mac 操作系统 High Sierra 10.13.6
以及以下网站来帮助我安装 pytrends https://pypi.org/project/pytrends/
我按照说明下载 pytrends 并安装了运行 pytrends 的要求“requests、lxml 和 pandas”。这是说明
- 安装pytrends
pip install pytrends
- 将pytrends连接到Google
来自 pytrends.request 导入 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)
最佳答案
从命令行安装 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/