概述

pytrends 函式庫中的 interest_by_region() 函數允許您擷取特定搜尋詞在不同地理位置的興趣資料。透過分析這些資料,您可以獲得不同地區搜尋詞受歡迎程度的寶貴洞察,從而為行銷和內容策略提供參考。

在本教學中,我們將介紹:

  1. 匯入必要的函式庫
  2. 設定 pytrends 請求
  3. 擷取按地區劃分的興趣資料
  4. 視覺化結果

安裝

要安裝 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