YouTube 的展示次数在发布后就会衰减:视频在几天内就在搜索可见度中达到顶峰,然后随着新内容的竞争而逐渐下降。跟踪这种衰减有助于创作者了解其内容的保质期。本教程使用每日 API 查询而不是依赖 YouTube Studio(仅显示您自己的视频)来跟踪一段时间内的搜索位置。
前置条件
- Python 3.8+
- Scavio API 密钥
- 要跟踪的视频 URL 或关键字
操作指南
步骤 1: 跟踪关键字的搜索位置
每天查询 YouTube 搜索并记录您的视频排名。
Python
import requests, os, json
from datetime import date
H = {'x-api-key': os.environ['SCAVIO_API_KEY'], 'Content-Type': 'application/json'}
def track_position(keyword: str, target_channel: str) -> dict:
resp = requests.post('https://api.scavio.dev/api/v1/search', headers=H,
json={'platform': 'youtube', 'query': keyword}, timeout=10)
results = resp.json().get('organic', [])
for i, r in enumerate(results):
if target_channel.lower() in r.get('channel', '').lower():
return {'keyword': keyword, 'position': i + 1, 'date': date.today().isoformat(),
'title': r.get('title', ''), 'views': r.get('views', '')}
return {'keyword': keyword, 'position': None, 'date': date.today().isoformat(), 'not_found': True}步骤 2: 建立每日跟踪循环
每天跟踪多个关键字并存储位置历史记录。
Python
from pathlib import Path
def daily_rank_track(keywords: list, channel: str) -> dict:
history_file = Path('yt_rank_history.json')
history = json.loads(history_file.read_text()) if history_file.exists() else {}
today = date.today().isoformat()
today_data = []
for kw in keywords:
position = track_position(kw, channel)
today_data.append(position)
history[today] = today_data
history_file.write_text(json.dumps(history, indent=2))
return {'date': today, 'tracked': len(keywords), 'found': sum(1 for d in today_data if d.get('position')),
'positions': today_data}步骤 3: 计算衰减率
分析位置历史记录以计算印象衰减曲线。
Python
def calculate_decay(history: dict, keyword: str) -> dict:
positions = []
for day, data in sorted(history.items()):
for entry in data:
if entry.get('keyword') == keyword and entry.get('position'):
positions.append({'date': day, 'position': entry['position']})
if len(positions) < 2:
return {'keyword': keyword, 'insufficient_data': True}
first_pos = positions[0]['position']
last_pos = positions[-1]['position']
days_tracked = len(positions)
decay_rate = (last_pos - first_pos) / days_tracked if days_tracked > 0 else 0
return {
'keyword': keyword,
'first_position': first_pos,
'current_position': last_pos,
'days_tracked': days_tracked,
'decay_rate_per_day': round(decay_rate, 2),
'positions_lost': last_pos - first_pos,
'trend': 'declining' if decay_rate > 0.1 else 'stable' if abs(decay_rate) < 0.1 else 'improving',
}步骤 4: 生成腐烂报告
生成一份总结报告,说明哪些内容衰减最快。
Python
def decay_report(history_file: str = 'yt_rank_history.json') -> dict:
history = json.loads(Path(history_file).read_text())
keywords = set()
for day_data in history.values():
for entry in day_data:
if entry.get('keyword'):
keywords.add(entry['keyword'])
decays = [calculate_decay(history, kw) for kw in keywords]
decays = [d for d in decays if not d.get('insufficient_data')]
decays.sort(key=lambda d: d.get('decay_rate_per_day', 0), reverse=True)
return {
'total_keywords': len(decays),
'fastest_decaying': decays[:3],
'most_stable': [d for d in decays if d.get('trend') == 'stable'],
'improving': [d for d in decays if d.get('trend') == 'improving'],
}Python 示例
Python
import requests, os, json
from datetime import date
def track_yt_rank(keyword, channel):
r = requests.post('https://api.scavio.dev/api/v1/search',
headers={'x-api-key': os.environ['SCAVIO_API_KEY'], 'Content-Type': 'application/json'},
json={'platform': 'youtube', 'query': keyword}).json()
for i, v in enumerate(r.get('organic',[])):
if channel.lower() in v.get('channel','').lower():
return i + 1
return NoneJavaScript 示例
JavaScript
async function trackYTRank(keyword, channel) {
const r = await fetch('https://api.scavio.dev/api/v1/search', {
method: 'POST', headers: {'x-api-key': process.env.SCAVIO_API_KEY, 'Content-Type': 'application/json'},
body: JSON.stringify({platform: 'youtube', query: keyword})
});
const results = (await r.json()).organic || [];
const idx = results.findIndex(v => v.channel?.toLowerCase().includes(channel.toLowerCase()));
return idx >= 0 ? idx + 1 : null;
}预期输出
JSON
A YouTube impression decay tracker that monitors search position daily and calculates decay rates per keyword, identifying content that needs refreshing.