了解如何使用Python在Data Studio中绘制K线走势图并跟踪Google趋势

Google趋势是一种免费且实用的工具,可为多种平台(例如网络搜索,Youtube或Google购物)提供多种语言的搜索兴趣,热门关键字和热门话题。无论使用哪种营销渠道,它都可以是非常有用的工具,可以获取宝贵的见解并为项目的下一步做出有意义的选择。

基本上,它提供了从2004年到现在的关键字相对流行度的数据,这真的很酷 (相对流行度是指您搜索字词的兴趣与在Google上搜索的所有关键字的兴趣之比。)

到目前为止,一切都很好,但是大规模分析Google趋势数据几乎是不切实际的。我们许多人使用率不高,因为在网站上搜索关键字并逐个获取数据点似乎是一项繁琐的工作。那么,我们如何才能更有效地使用Google趋势?

在本文中,我的目的是向您展示Python中的pytrends库以及在数据分析中可以从中获得什么好处。我还将解释Google Spreadsheets和Jupyter Notebook之间的连接,以便将数据导入Google Data Studio中以轻松地与他人共享。例如,在Data Studio仪表板上分析Search Console数据时,将Google趋势数据存储在同一页面上不是很好吗?如果您的回答是肯定的,那就让我们深入探讨

我将在本文中介绍3个主题:

  • 使用Pytrends库进行编码并探索其功能
  • 使用gspread库将Jupyter Notebook连接到Google Spreadsheets
  • 将数据导入Google Data Studio

使用Pytrends库的系统要求

  • Python 2.7+和Python 3.3+
  • 需要Requests,lxml,Pandas库。如果您不知道如何安装库,请查看此Python文档。 (提示:pip install pandas)
  • Jupyter Notebook是一个开源Web应用程序,提供了运行代码的环境。

用Pytrends库编码

首先,您必须安装该库:

pip install pytrends

导入必要的库:

import pytrends
from pytrends.request import TrendReq
import pandas as pd
import time
import datetime
from datetime import datetime, date, time

现在是时候编写代码了

pytrend = TrendReq()
pytrend.build_payload(kw_list=['tea', 'coffee', 'coke', 'milk', 'water'], timeframe='today 12-m', geo = 'GB')

有效负载功能对于指定搜索很重要。写下您的关键字,确定日期范围,位置以及许多其他内容,例如选择要分析的YouTube或购物频道。在上面的代码中,“今天12时”表示一年的数据。您可以通过使用“ geo”指定位置来缩小搜索范围。

假设您有一个YouTube频道,并且只想查看YouTube搜索趋势。然后您的代码将如下所示:

pytrend.build_payload(kw_list=['tea', 'coffee', 'coke', 'milk', 'water'], timeframe='today 12-m', geo = 'GB', gprop= youtube)

或者,假设您有一个饮食博客,并且想要获取该类别中关键字的趋势数据,而不是相对于所有搜索。然后将是这样的:

pytrend.build_payload(kw_list=['tea', 'coffee', 'coke', 'milk', 'water'], timeframe='today 12-m', geo = 'GB', cat = 71)

为了查看所有功能和过滤器,您应该在Github上检查此存储库,并且您还可以在此处找到所有类别代码。

(顺便说一句,请注意,您不能在此处直接编写5个以上的关键字。由于您只能比较Google趋势中的5个关键字,因此会出现错误。我将使用其他代码来分析5个以上的关键字。)

因此,让我们继续并获取趋势得分。

#to get interest over time score, you'll need pytrend.interest_over_time() function.
#For more functions, check this: https://github.com/GeneralMills/pytrends
interest_over_time_df = pytrend.interest_over_time() print(interest_over_time_df.head())

# Let's draw
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
dx = interest_over_time_df.plot.line(figsize = (9,6), title = "Interest Over Time")
dx.set_xlabel('Date')
dx.set_ylabel('Trends Index')
dx.tick_params(axis='both', which='major', labelsize=13)

建议关键字

现在,我将向您展示Google趋势的另一个很酷的功能。如果您使用建议功能,它将返回建议的关键字及其“类型”。

print(pytrend.suggestions(keyword='search engine land'), 'n')
print(pytrend.suggestions(keyword='amazon'), 'n')
print(pytrend.suggestions(keyword='cats'), 'n')
print(pytrend.suggestions(keyword='macbook pro'), 'n')
print(pytrend.suggestions(keyword='beer'), 'n')
print(pytrend.suggestions(keyword='ikea'), 'n')

相关查询

这是我最喜欢的尤其是因为它对Google Ads,关键字研究和内容创建非常有帮助。

让我们检查“美容”类别中的“基础”关键字,并获取相关的关键字。

pytrend.build_payload(kw_list=['foundation'], geo = 'US', timeframe = 'today 3-m', cat = 44)
related_queries= pytrend.related_queries()
print(related_queries)

您将在输出中看到两部分;热门关键字和上涨关键字。排名靠前的关键字的值显示Google趋势得分从0到100。但是,上涨的关键字的值显示对关键字的兴趣增加了多少。

如果网站出售基金会,那么关注人们最近在搜索什么,那就好了吗?这些产品可能会变得流行或逆转,它们最近的声誉可能很差,这就是人们搜索它们的原因。例如,尽快在Google Ads中注意到这一点可能会阻止您花费过多的钱而没有获得转化。

跟踪大量关键字

现在,我将在此处编写一组随机关键字并获取其数据。您也可以从csv或excel文件中读取关键字,但请确保其类型必须为“列表”。

searches = ['detox', 'water fasting', 'benefits of fasting', 'fasting benefits',
'acidic', 'water diet', 'ozone therapy', 'colon hydrotherapy', 'water fast',
'reflexology', 'balance', 'deep tissue massage', 'cryo', 'healthy body', 'what is detox',
'the truth about cancer', 'dieta', 'reverse diabetes', 'how to reverse diabetes',
'water cleanse', 'can you drink water when fasting', 'water fasting benefits', 'glycemic load', 'anti ageing', 'how to water fast', 'ozone treatment', 'healthy mind', 'can you reverse diabetes', 'anti aging', 'health benefits of fasting', 'hydrocolonic', 'shiatsu massage', 'seaweed wrap', 'shiatsu',
'can you get rid of diabetes', 'how to get rid of diabetes', 'healthy body healthy mind', 'colonic hydrotherapy', 'green detox', 'what is water fasting', '21 day water fast', 'benefits of water fasting', 'cellulite', 'ty bollinger', 'detox diet', 'detox program', 'anti aging treatments', 'ketogenic', 'glycemic index', 'water fasting weight loss', 'keto diet plan', 'acidic symptoms', 'alkaline diet', 'water fasting diet', 'laser therapy', 'anti cellulite massage', 'swedish massage', 'benefit of fasting', 'detox your body', 'colon therapy', 'reversing diabetes', 'detoxing', 'truth about cancer', 'how to remove acidity from body', '21 day water fast results', 'colon cleanse', 'fasting health benefits', 'antiaging', 'aromatheraphy massage']

groupkeywords = list(zip(*[iter(searches)]*1))
groupkeywords = [list(x) for x in groupkeywords]

dicti = {}
i = 1
for trending in groupkeywords:
pytrend.build_payload(trending, timeframe = 'today 3-m', geo = 'GB')
dicti[i] = pytrend.interest_over_time()
i+=1

result = pd.concat(dicti, axis=1)
result.columns = result.columns.droplevel(0)
result = result.drop('isPartial', axis = 1)

result

是!我拥有所有这些,但是如果要将数据与Search Console合并,则需要重塑数据框。

result.reset_index(level=0, inplace=True)
pd.melt(result, id_vars='date', value_vars=searches)

result.to_excel(‘trends.xlsx’)

Google趋势数据已准备就绪

使用gspread库将Jupyter Notebook连接到Google Spreadsheets

首先,您需要启用一些API并创建一个秘密客户端JSON文件,以授权Google表格访问权限。我不会在本文中对此进行解释,但是这是一个很好的指南,解释了如何逐步执行该操作。

然后,您可以在下面使用以下代码:

import gspread
from oauth2client.service_account import ServiceAccountCredentials
links = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
credentials =
ServiceAccountCredentials.from_json_keyfile_name('ENTER-YOUR-JSON-FILE-NAME-HERE.json', links)
gc = gspread.authorize(credentials)

创建和打开电子表格:

sh = gc.create('My cool spreadsheet')
wks = gc.open("My cool spreadsheet").sheet1
# check colab documents here for more examples →
https://colab.research.google.com/notebooks/io.ipynb

创建一个自定义公式以将数据帧发送到工作表中:

#https://www.danielecook.com/from-pandas-to-google-sheets/

def iter_pd(df):
for val in list(df.columns):
yield val
for row in df.values:
for val in list(row):
if pd.isna(val):
yield ""
else:
yield val

def pandas_to_sheets(pandas_df, sheet, clear = True):
# Updates all values in a workbook to match a pandas dataframe if clear:
sheet.clear()
(row, col) = pandas_df.shape
cells = sheet.range("A1:
{}".format(gspread.utils.rowcol_to_a1(row + 1, col)))
for cell, val in zip(cells, iter_pd(df)):
cell.value = val
sheet.update_cells(cells)

查看其工作方式的示例:

df = pd.read_csv("train.csv")
pandas_to_sheets(df, wks)

让我们继续趋势数据并将其与Search Console数据合并。

sh = gc.create('GoogleTrends')
wks = gc.open("GoogleTrends").sheet1
pandas_to_sheets(result, wks)

dx = pd.read_excel('Trends.xlsx', sheet_name='Sheet1')
dz = pd.read_excel('Trends.xlsx', sheet_name = 'console') #my console data is here, make sure where yours is
dm = pd.merge(dx, dz, on = ['Query', 'Date'])
dm

并将其发送到Google表格中。

wks = gc.open("GoogleTrends").sheet3
pandas_to_sheets(dm, wks)

将数据导入Google Data Studio

现在,您可以将此电子表格与Google Data Studio连接起来:

追踪上涨关键字

pytrend.build_payload(kw_list=['foundation', 'eyeliner', 'concealer', 'lipstick'], geo = 'US', timeframe = 'today 3-m', cat = 44)
related_queries= pytrend.related_queries()
dg=related_queries.get('lipstick').get('rising')
dg

再次使用pandas_to_sheets。将它们导入Data Studio并可视化:

总结

乍一看似乎很复杂,但是只需尝试这些代码并创建自己的仪表板即可。因为最后,您只需要在Jupyter Notebook上运行代码并在Google Data Studio上刷新数据即可。我保证,更新所有这些只需10-15秒

这是我所有Python代码的Github存储库。

编码愉快

本文表达的观点是来宾作者的观点,不一定是Search Engine Land。此处列出了工作人员作者。

资讯来源:由0x资讯编译自SEARCHENGINELAND,版权归作者Hülya Çoban所有,未经许可,不得转载
提示:投资有风险,入市需谨慎,本资讯不作为投资理财建议。请理性投资,切实提高风险防范意识;如有发现的违法犯罪线索,可积极向有关部门举报反映。
你可能还喜欢