
FastAPI是什么?快速上手指南
工具 | 支持功能 | 優(yōu)劣勢 |
---|---|---|
PyTrends | 時序趨勢、地域熱度、相關(guān)查詢、熱門趨勢 | 免費開源,社區(qū)活躍;易受 Google 反爬限流 |
SerpAPI Trends | RESTful API 返回 JSON,支持實時趨勢、熱門趨勢 | 商用穩(wěn)定,付費模式;支持多引擎搜索趨勢 |
SearchAPI.io | 聚合多平臺趨勢接口,支持電商/社交/網(wǎng)頁熱門趨勢 | 數(shù)據(jù)覆蓋廣;依賴第三方服務(wù),需付費 |
Apify Scraper | 可視化配置,無需編程即可抓取熱門趨勢 | 便捷易用;速率與穩(wěn)定性依賴服務(wù)質(zhì)量 |
推薦方案:若偏好免費且可擴展,選擇 PyTrends;若需企業(yè)級 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))
# 構(gòu)建時序數(shù)據(jù)請求,過去 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 參數(shù)
now 1-H
:過去一小時now 7-d
:過去七天today 12-m
:過去一年geo 參數(shù)
geo=''
geo='US'
、geo='CN'
category 參數(shù)
cat=34
(游戲)、cat=37
(藝術(shù))實時趨勢接口
pytrends.realtime_trending_searches(pn='GLOBAL')
獲取全球?qū)崟r趨勢rt = pytrends.realtime_trending_searches(pn='GLOBAL')
print("全球?qū)崟r趨勢:", rt.head(15))
腳本化封裝
def fetch_trends(keywords, timeframe, geo):
pytrends.build_payload(keywords, timeframe=timeframe, geo=geo)
return pytrends.interest_over_time()
調(diào)度執(zhí)行
cron
、Airflow、Jenkins 定時觸發(fā)異常與重試
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('搜索指數(shù)')
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()
歸一化與錨點校準(zhǔn)
流量模式識別
rolling(window=3)
) 平滑異常波動反爬策略
配額管理
合規(guī)性
通過本文方法,你將掌握如何使用 Google Trends API 獲取熱門趨勢數(shù)據(jù),打造一套自動化、可視化、數(shù)據(jù)驅(qū)動的 SEO 與市場調(diào)研體系。立即行動,搶占趨勢先機!
原文引自YouTube視頻:https://www.youtube.com/watch?v=fbzuhIaVsFU