抖音用户主页视频数据爬虫详解:点赞、收藏、分享字段抓取与 TaoToken 配置骨架
发布时间:2026/9/26 17:01:03来源:尧图网络
1. 抖音主页视频列表接口到底长什么样抖音用户主页的视频数据本质上是一个分页拉取的 POST 接口。你打开任意一个博主主页往下滚动浏览器就会不断向服务端要下一页数据。这个接口返回的 JSON 里除了视频标题、封面、时长还带着点赞数、收藏数、分享数、评论数这些互动字段。很多人第一次抓的时候以为点赞收藏是单独接口其实它们就藏在视频列表的statistics结构里一次请求全给你。这篇要解决的问题很具体怎么在本地稳定复现「输入一个博主主页链接 → 拿到他全部视频的点赞/收藏/分享数据 → 落成表格」。适合有 Python 基础、想练接口分析但不想碰复杂逆向的人。我会把请求参数拆开讲清楚给出可复制的config.toml和settings.json骨架最后用 TaoToken 统一 Key 通道做鉴权验证让整条链路跑通。需要提前说明的是抖音的sec_user_id是加密的max_cursor是翻页游标has_more决定要不要继续请求。这三个东西不理解脚本一定写不对。下面按「先看懂接口 → 再配环境 → 再写配置 → 再验证 → 再排错」的顺序走。2. 抓包看清请求参数与响应结构2.1 找到那个 POST 包打开目标博主主页F12 切到 Network筛选 Fetch/XHR然后往下滚动页面。你会看到一串请求其中有一个以post开头、路径里带/aweme/v1/web/aweme/post/的接口就是主页视频列表。右键复制为 cURL丢进你习惯的请求分析工具里先把 headers 和 params 完整还原出来。2.2 参数删减哪些是必需的把 cURL 转成请求后逐个删参数测试。实测下来真正必需的只有这几个参数作用是否加密sec_user_id博主唯一标识是需从主页 URL 提取max_cursor翻页游标首页传 0否count期望返回条数否device_platform设备标识否aid应用 ID否这里有个坑你把count改成 100返回的并不一定是 100 条。抖音服务端有自己的返回节奏实际条数以响应里的数组长度为准。所以别用count判断是否抓完要用has_more。2.3 max_cursor 与 has_more 的翻页逻辑响应 JSON 里有两个关键字段max_cursor下一次请求要带的游标值直接取本次响应里的这个字段。has_more为 1 表示还有下一页为 0 表示到底了。翻页判断写成这样最稳next_cursor data.get(max_cursor, 0) has_more data.get(has_more, 0) if has_more 1: params[max_cursor] next_cursor else: break2.4 互动字段藏在哪每条视频对象里有个statistics字段结构大致如下{ statistics: { digg_count: 12345, collect_count: 678, share_count: 90, comment_count: 456, play_count: 78900 } }digg_count是点赞collect_count是收藏share_count是分享comment_count是评论。提取时直接按 key 取别去猜字段名。2.5 sec_user_id 怎么拿它就在博主主页 URL 里形如https://www.douyin.com/user/MS4wLjABAAAA...user/后面那串就是。用正则匹配即可import re def extract_sec_user_id(url: str) - str: match re.search(r/user/([A-Za-z0-9_\-]), url) if not match: raise ValueError(URL 中未找到 sec_user_id) return match.group(1)如果你要批量抓多个博主可以用自动化浏览器打开主页等页面加载后从当前 URL 里正则提取避免手动复制。3. TaoToken 前置统一 Key 与 API 通道3.1 为什么这里要接 TaoToken抖音接口的 Cookie 很容易过期批量抓的时候经常跑到一半就 401。我的做法是把请求鉴权统一走 TaoToken 的 API 通道用一个 Key 管理所有请求Cookie 失效时集中刷新不用在每个脚本里散落一堆凭证。TaoToken 官网是 https://taotoken.net/?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content API 入口是 https://taotoken.net/api 。3.2 拿 Key 的路径进入控制台创建 API Key地址是 https://taotoken.net/console/api-keys?utm_sourcetaotoken_aicg_blog_endutm_contentapi_keysutm_campaignrewrite 。创建后复制保存后面写进config.toml。如果你还想顺手验证模型通道是否通可以去模型对话页 https://taotoken.net/model-chat?utm_sourcetaotoken_aicg_blog_endutm_contentmodel_chatutm_campaignrewrite 发一条测试消息。3.3 接入文档位置请求头格式、鉴权方式、错误码说明都在接入文档里https://taotoken.net/doc?utm_sourcetaotoken_aicg_blog_endutm_contentdocutm_campaignrewrite 。写配置前先扫一遍能省很多排错时间。4. 可复制的 config.toml 与 settings.json 骨架4.1 config.toml# config.toml [taotoken] api_base https://taotoken.net/api api_key sk-你的Key timeout 30 [spider] base_url https://www.douyin.com/aweme/v1/web/aweme/post/ count 20 max_pages 50 sleep_min 1.5 sleep_max 3.0 [output] format excel path ./output/douyin_videos.xlsx [headers] user_agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 referer https://www.douyin.com/4.2 settings.json{ targets: [ { name: 示例博主A, homepage: https://www.douyin.com/user/MS4wLjABAAAA示例, sec_user_id: } ], fields: [digg_count, collect_count, share_count, comment_count], retry: { times: 3, backoff: 2 }, cookie_refresh: { enabled: true, trigger_status: [401, 403] } }sec_user_id留空时脚本会用正则从homepage自动提取。fields决定导出哪些互动列想加播放量就补play_count。4.3 请求构造与鉴权注入import toml import json import requests config toml.load(config.toml) settings json.load(open(settings.json, encodingutf-8)) def build_headers(cfg): return { User-Agent: cfg[headers][user_agent], Referer: cfg[headers][referer], Authorization: fBearer {cfg[taotoken][api_key]} } def fetch_page(sec_user_id, cursor, cfg): params { sec_user_id: sec_user_id, max_cursor: cursor, count: cfg[spider][count], device_platform: webapp, aid: 6383 } resp requests.post( cfg[spider][base_url], paramsparams, headersbuild_headers(cfg), timeoutcfg[taotoken][timeout] ) resp.raise_for_status() return resp.json()5. 验证请求与成功结果5.1 单页验证先只抓一页确认字段能取到data fetch_page(MS4wLjABAAAA示例, 0, config) items data.get(aweme_list, []) print(本页条数:, len(items)) for it in items[:3]: stat it.get(statistics, {}) print(it.get(desc, )[:20], stat.get(digg_count), stat.get(collect_count), stat.get(share_count))跑通后你会看到类似输出本页条数: 20 今天分享一个技巧 12345 678 90 实测有效的配置方法 9876 543 215.2 翻页验证cursor 0 all_items [] for page in range(config[spider][max_pages]): data fetch_page(MS4wLjABAAAA示例, cursor, config) items data.get(aweme_list, []) all_items.extend(items) if data.get(has_more) ! 1: break cursor data.get(max_cursor, 0) print(累计条数:, len(all_items))5.3 导出 Excelimport pandas as pd rows [] for it in all_items: stat it.get(statistics, {}) rows.append({ desc: it.get(desc, ), digg_count: stat.get(digg_count, 0), collect_count: stat.get(collect_count, 0), share_count: stat.get(share_count, 0), comment_count: stat.get(comment_count, 0) }) pd.DataFrame(rows).to_excel(config[output][path], indexFalse) print(导出完成:, config[output][path])6. 本篇常见错排查6.1 返回空列表或 has_more 一直为 1多半是sec_user_id提取错了或者max_cursor没更新。检查正则是否匹配到完整串检查翻页时是否把响应里的max_cursor赋给了下一次请求。6.2 401 / 403 频繁出现Cookie 过期是主因。把cookie_refresh.enabled设为 true触发状态码时重新走自动化浏览器登录刷新。如果还是不行去接入文档 https://taotoken.net/doc?utm_sourcetaotoken_aicg_blog_endutm_contentdocutm_campaignrewrite 核对鉴权头格式确认 Key 没写错。6.3 count 设 100 但只返回 20 条这是服务端行为不是 bug。以aweme_list实际长度为准别用count判断抓取量。6.4 导出 Excel 中文乱码to_excel默认编码没问题乱码通常出在打开方式。用 pandas 直接写.xlsx即可别中途转 CSV 再转回来。6.5 批量抓取中途断掉给请求加 retry 和退避settings.json里的retry.times和backoff就是干这个的。再配合sleep_min/sleep_max随机间隔稳定性会好很多。7. 把鉴权通道固定下来整条链路里最容易出问题的不是字段解析而是鉴权。Cookie 会过期Key 要统一管理。我的建议是把 TaoToken 的 API Key 作为唯一凭证入口所有请求走同一个通道Cookie 刷新逻辑集中在一处。这样你换博主、加字段、改导出格式都不用动鉴权代码。如果你后面要做长期编码或 Agent 自动化可以看 Coding Plan https://taotoken.net/coding-plan?utm_sourcetaotoken_aicg_blog_endutm_contentcoding_planutm_campaignrewrite 把请求调度和重试策略固化下来。需要新建或轮换 Key 时直接去 https://taotoken.net/console/api-keys?utm_sourcetaotoken_aicg_blog_endutm_contentapi_keysutm_campaignrewrite 操作。配置骨架已经给你了先跑通单页再开翻页最后接导出顺序别乱。
网站建设高端定制企业官网