概述
pytrends 库中的 interest_by_region() 函数允许您检索特定搜索词在不同地理位置的兴趣数据。通过分析这些数据,您可以获得不同地区搜索词受欢迎程度的宝贵洞察,从而为营销和内容策略提供参考。
在本教程中,我们将介绍:
- 导入必要的库
- 设置
pytrends请求 - 检索按地区划分的兴趣数据
- 可视化结果
安装
要安装Pytrends,只需使用pip:
pip install matplotlib
检索按地区划分的兴趣数据
首先,我们需要导入必要的库并设置 pytrends 请求。
from pytrends.request import TrendReq
import pandas as pd
# Set up pytrends request
pytrends = TrendReq(hl='en-US', tz=360)
接下来,我们将使用 build_payload() 函数指定搜索词和时间范围,然后使用 interest_by_region() 函数检索按地区划分的兴趣数据。
keywords = ['Python']
timeframe = '2023-01-01 2023-03-31'
# Build payload
pytrends.build_payload(keywords, timeframe=timeframe, geo='')
# Retrieve interest by region data
region_interest = pytrends.interest_by_region(resolution='COUNTRY', inc_low_vol=True, inc_geo_code=False)
这将返回一个DataFrame,其中包含搜索词’Python’在2023年第一季度按地区划分的兴趣数据。
可视化结果
现在,我们可以使用条形图来可视化按地区划分的兴趣数据。
import matplotlib.pyplot as plt
# Sort the data by interest value
region_interest = region_interest.sort_values(by='Python', ascending=False)
# Plot the interest by region data
plt.figure(figsize=(12, 6))
plt.bar(region_interest.index, region_interest['Python'])
plt.xlabel('Country')
plt.ylabel('Interest')
plt.title('Interest by Region for Python (Q1 2023)')
plt.xticks(rotation=90)
plt.show()
此图显示了搜索词’Python’在不同国家/地区的兴趣,让您能够识别该词特别受欢迎的地区。
结论
在本文中,我们演示了如何使用 pytrends 库中的 interest_by_region() 函数分析特定搜索词按地区划分的兴趣。通过探索这些数据,您可以获得不同地理位置搜索词受欢迎程度的有针对性洞察,帮助您更好地理解受众并优化营销策略。本教程涵盖了从设置 pytrends 请求到可视化结果的整个收集和分析按地区划分的兴趣数据的过程。
NOTE : pytrends uses an unofficial API. Please use here for issues.
SAMPLE CODE : https://github.com/hobbyworker/google-trend-for-python