
FastAPI是什么?快速上手指南
工具 | 支持功能 | 優劣勢 |
---|---|---|
PyTrends | 時序趨勢、地域熱度、相關查詢、熱門趨勢 | 免費開源,社區活躍;易受 Google 反爬限流 |
SerpAPI Trends | RESTful API 返回 JSON,支持實時趨勢、熱門趨勢 | 商用穩定,付費模式;支持多引擎搜索趨勢 |
SearchAPI.io | 聚合多平臺趨勢接口,支持電商/社交/網頁熱門趨勢 | 數據覆蓋廣;依賴第三方服務,需付費 |
Apify Scraper | 可視化配置,無需編程即可抓取熱門趨勢 | 便捷易用;速率與穩定性依賴服務質量 |
推薦方案:若偏好免費且可擴展,選擇 PyTrends;若需企業級 SLA 與多平臺覆蓋,選用 SerpAPI Trends 或 SearchAPI.io。
# 安裝 PyTrends 和依賴
pip install pytrends pandas matplotlib
pip install google-search-results
export SERPAPI_API_KEY="你的_serpapi_key"
from pytrends.request import TrendReq
# 初始化,hl='zh-CN'、tz=8
pytrends = TrendReq(hl='zh-CN', tz=8)
# 獲取中國熱門搜索主題(Hot Trends)
hot_trends = pytrends.trending_searches(pn='china')
print("實時熱門趨勢:", hot_trends.head(20))
# 構建時序數據請求,過去 30 天
pytrends.build_payload(['ChatGPT'], timeframe='now 7-d', geo='CN')
df_time = pytrends.interest_over_time()
print(df_time['ChatGPT'].tail())
# 按城市分辨率獲取地域分布
df_region = pytrends.interest_by_region(resolution='CITY', inc_low_vol=True)
print(df_region.sort_values('ChatGPT', ascending=False).head(10))
related = pytrends.related_queries()
print("Top 查詢:", related['ChatGPT']['top'].head(10))
print("Rising 查詢:", related['ChatGPT']['rising'].head(10))
timeframe 參數
now 1-H
:過去一小時now 7-d
:過去七天today 12-m
:過去一年geo 參數
geo=''
geo='US'
、geo='CN'
category 參數
cat=34
(游戲)、cat=37
(藝術)實時趨勢接口
pytrends.realtime_trending_searches(pn='GLOBAL')
獲取全球實時趨勢rt = pytrends.realtime_trending_searches(pn='GLOBAL')
print("全球實時趨勢:", rt.head(15))
腳本化封裝
def fetch_trends(keywords, timeframe, geo):
pytrends.build_payload(keywords, timeframe=timeframe, geo=geo)
return pytrends.interest_over_time()
調度執行
cron
、Airflow、Jenkins 定時觸發異常與重試
import matplotlib.pyplot as plt
plt.figure(figsize=(10,5))
plt.plot(df_time.index, df_time['ChatGPT'], label='ChatGPT 熱度')
plt.title('ChatGPT 過去7天搜索趨勢')
plt.xlabel('日期')
plt.ylabel('搜索指數')
plt.legend()
plt.show()
import plotly.express as px
fig = px.choropleth(df_region.reset_index(), locations='geoName',
locationmode='USA-states', color='ChatGPT',
scope='asia', title='ChatGPT 地域熱度分布')
fig.show()
歸一化與錨點校準
流量模式識別
rolling(window=3)
) 平滑異常波動反爬策略
配額管理
合規性
通過本文方法,你將掌握如何使用 Google Trends API 獲取熱門趨勢數據,打造一套自動化、可視化、數據驅動的 SEO 與市場調研體系。立即行動,搶占趨勢先機!
原文引自YouTube視頻:https://www.youtube.com/watch?v=fbzuhIaVsFU