通过 Scavio API 获取 TikTok 用户最近的帖子,包括播放计数、点赞计数、评论计数、分享计数和视频描述,每页结果 0.005 美元。使用 max_cursor 分页来获取第一页以外的内容。
前置条件
- Scavio API 密钥
- 目标用户的 sec_uid(来自配置文件端点)
- Python 3.8+ 或 Node.js 18+
操作指南
步骤 1: 获取用户sec_uid
首先调用配置文件端点以获取帖子所需的 sec_uid。
Python
import requests, os
HEADERS = {'Authorization': f'Bearer {os.environ["SCAVIO_API_KEY"]}',
'Content-Type': 'application/json'}
profile = requests.post('https://api.scavio.dev/api/v1/tiktok/profile',
headers=HEADERS, json={'username': 'target_user'}).json()
sec_uid = profile['data']['user']['sec_uid']步骤 2: 获取帖子的第一页
使用 sec_user_id 和 count 调用 user/posts 端点。
Python
resp = requests.post('https://api.scavio.dev/api/v1/tiktok/user/posts',
headers=HEADERS,
json={'sec_user_id': sec_uid, 'count': 20})
data = resp.json()['data']
for video in data.get('videos', []):
print(f"{video['desc'][:50]} | {video['stats']['playCount']:,} plays")步骤 3: 使用 max_cursor 进行分页
使用响应中的 max_cursor 来获取下一页。
Python
if data.get('has_more'):
next_page = requests.post(
'https://api.scavio.dev/api/v1/tiktok/user/posts',
headers=HEADERS,
json={'sec_user_id': sec_uid, 'count': 20,
'max_cursor': data['max_cursor']})
page2 = next_page.json()['data']
print(f'Page 2: {len(page2.get("videos", []))} videos')Python 示例
Python
import requests, os
HEADERS = {'Authorization': f'Bearer {os.environ["SCAVIO_API_KEY"]}',
'Content-Type': 'application/json'}
def get_all_posts(username, max_pages=5):
profile = requests.post('https://api.scavio.dev/api/v1/tiktok/profile',
headers=HEADERS, json={'username': username}).json()
sec_uid = profile['data']['user']['sec_uid']
videos = []
params = {'sec_user_id': sec_uid, 'count': 20}
for _ in range(max_pages):
resp = requests.post('https://api.scavio.dev/api/v1/tiktok/user/posts',
headers=HEADERS, json=params).json()['data']
videos.extend(resp.get('videos', []))
if not resp.get('has_more'):
break
params['max_cursor'] = resp['max_cursor']
return videos
videos = get_all_posts('target_user')
print(f'Fetched {len(videos)} videos')JavaScript 示例
JavaScript
const H = {'Authorization': `Bearer ${process.env.SCAVIO_API_KEY}`, 'Content-Type': 'application/json'};
async function getPosts(username, maxPages = 5) {
const profile = await fetch('https://api.scavio.dev/api/v1/tiktok/profile', {
method: 'POST', headers: H, body: JSON.stringify({username})
}).then(r => r.json());
const secUid = profile.data.user.sec_uid;
const videos = [];
let params = {sec_user_id: secUid, count: 20};
for (let i = 0; i < maxPages; i++) {
const r = await fetch('https://api.scavio.dev/api/v1/tiktok/user/posts', {
method: 'POST', headers: H, body: JSON.stringify(params)
}).then(r => r.json());
videos.push(...(r.data.videos || []));
if (!r.data.has_more) break;
params.max_cursor = r.data.max_cursor;
}
return videos;
}
getPosts('target_user').then(v => console.log(`${v.length} videos`));预期输出
JSON
Array of video objects with description, play count, like count, comment count, share count, and video metadata for each post.