feat(monitor): 抖音 Web 接口客户端 —— 绕开爬虫子进程,直接发 HTTP
移植自 mac-agent-os 的 mediacrawler_adapter:不起子进程、不开页面,用浏览器里那份
登录态直接调抖音 Web 接口。产物键名照抄 store/douyin,所以 ingest 那条链路一个字不用改。
**目前能用的(真环境实测,非推断)**:
profile/other : 200, 7075 字节 —— 博主主页指标(粉丝/获赞/作品数/昵称)
aweme/detail : 200, 45425 字节 —— 单条作品详情(含点赞/评论/收藏/分享)
**目前不能用的:作品列表 `aweme/post`。** 两个互相独立的原因:
1. 这个接口被抖音单独升级成了真校验:不带 x-tt-argus 回 403「Uifid Not Found」,
带上 dummy 值回 200 + **空 body**。也就是说「头在不在」骗得过,「真校验」过不了。
同一套头打 profile/other 和 aweme/detail 都是通的 —— 抖音是挑着接口加保护的,
挑中的恰好是「批量拉作品列表」这个最敏感的动作。
2. 改走页面截获也不行:CDP 浏览器打开博主主页会落到「验证码中间页」(当天大量探测的
代价,过几小时要重测)。
所以现在的边界是:**已知作品的指标刷新能做,自动发现新作品做不了**。
**排查中控住变量后得到的两条事实**(都写进注释了):
· `Accept` / `Accept-Language` / `Referer` 才是主页接口能返回真数据的原因 —— 只有
UA+client hints+Cookie 时是 200 但仅 121 字节的空壳,补上这三个头变 7074 字节。
(我先前猜的 sec-ch-ua 不是关键。)
· 因此 UA 与 client hints 必须**成套地取自同一个浏览器**,所以 BrowserIdentity 一次
从 CDP 取齐 cookie + UA + hints,而不是各自写死。
「200 + 空 body 必须当场报错」也是刻意写死的:放过去它会在下游变成「这个博主没作品」,
把一次失败伪装成一条正常结果 —— 爬虫那条路正是这么栽的,还被翻译成「账号被封」。
测试 +11:cookie 解析、请求头成套性(含 uifid 缺失/回退)、产物键名与 store 对齐、
以及 _get 的三条失败路径(空 body / 403 带网关原话 / 正常返回)。
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
"""
|
||||
agentos crawl — 内容采集(网页/视频/提取/调度)
|
||||
|
||||
封装 05_crawl/ 目录下的采集脚本 + web_crawler 技能。
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from agentos.base import AgentOSPlugin
|
||||
|
||||
AGENT_SYNC = Path(__file__).resolve().parent.parent.parent.parent.parent
|
||||
CRAWL_DIR = AGENT_SYNC / "05_tools" / "05_crawl"
|
||||
|
||||
|
||||
class CrawlPlugin(AgentOSPlugin):
|
||||
name = "crawl"
|
||||
description = "内容采集 — 网页/视频/提取/调度"
|
||||
nav = {
|
||||
'group': '内容采集',
|
||||
'icon': '📡',
|
||||
'order': 4,
|
||||
'items': [
|
||||
{'view': 'crawl-tasks', 'label': '采集任务'},
|
||||
{'view': 'crawl-sources', 'label': '源管理'},
|
||||
{'view': 'crawl-history', 'label': '采集历史'},
|
||||
]
|
||||
}
|
||||
|
||||
def _run_script(self, script_name: str, args: list[str] = None) -> int:
|
||||
"""运行 crawl 目录下的 Python 脚本"""
|
||||
script_path = CRAWL_DIR / script_name
|
||||
if not script_path.exists():
|
||||
print(f"脚本不存在: {script_path}")
|
||||
return 1
|
||||
cmd = [sys.executable, str(script_path)] + (args or [])
|
||||
result = subprocess.run(cmd, cwd=str(CRAWL_DIR))
|
||||
return result.returncode
|
||||
|
||||
def register(self, subparsers):
|
||||
p = subparsers.add_parser("crawl", help=self.description)
|
||||
p_sub = p.add_subparsers(dest="crawl_action", help="采集操作")
|
||||
|
||||
# web - 网页抓取
|
||||
pw = p_sub.add_parser("web", help="网页内容抓取")
|
||||
pw.add_argument("url", nargs="?", help="目标 URL")
|
||||
pw.add_argument("--output", help="输出文件路径")
|
||||
pw.add_argument("--depth", type=int, default=1, help="抓取深度")
|
||||
pw.set_defaults(crawl_func=self.cmd_web)
|
||||
|
||||
# video - 视频下载
|
||||
pv = p_sub.add_parser("video", help="视频下载")
|
||||
pv.add_argument("url", nargs="?", help="视频 URL")
|
||||
pv.add_argument("--format", default="mp4", help="输出格式")
|
||||
pv.set_defaults(crawl_func=self.cmd_video)
|
||||
|
||||
# extract - 信息提取
|
||||
pe = p_sub.add_parser("extract", help="信息提取")
|
||||
pe.add_argument("file", nargs="?", help="目标文件路径")
|
||||
pe.add_argument("--fields", help="提取字段 (逗号分隔)")
|
||||
pe.add_argument("--url", help="从 URL 提取")
|
||||
pe.set_defaults(crawl_func=self.cmd_extract)
|
||||
|
||||
# schedule - 定时采集
|
||||
psc = p_sub.add_parser("schedule", help="采集定时任务")
|
||||
psc.add_argument("action", nargs="?", default="list",
|
||||
choices=["list", "add", "remove"],
|
||||
help="操作")
|
||||
psc.set_defaults(crawl_func=self.cmd_schedule)
|
||||
|
||||
# source - 采集源管理
|
||||
ps = p_sub.add_parser("source", help="采集源管理")
|
||||
ps.add_argument("action", nargs="?", default="list",
|
||||
choices=["list", "add", "remove", "test"],
|
||||
help="操作")
|
||||
ps.add_argument("--name", help="源名称")
|
||||
ps.add_argument("--url", dest="src_url", help="源URL")
|
||||
ps.add_argument("--type", dest="src_type",
|
||||
choices=["rss", "web", "api", "social"],
|
||||
help="源类型")
|
||||
ps.set_defaults(crawl_func=self.cmd_source)
|
||||
|
||||
# content-inspiration - 内容灵感采集
|
||||
pci = p_sub.add_parser("inspiration", help="内容灵感采集")
|
||||
pci.add_argument("action", nargs="?", default="daily",
|
||||
choices=["daily", "trending", "search"],
|
||||
help="操作")
|
||||
pci.add_argument("--keyword", help="搜索关键词")
|
||||
pci.set_defaults(crawl_func=self.cmd_inspiration)
|
||||
|
||||
return p
|
||||
|
||||
def dispatch(self, args):
|
||||
if hasattr(args, 'crawl_func'):
|
||||
return args.crawl_func(args)
|
||||
print("未知采集命令,使用 agentos crawl --help")
|
||||
return 1
|
||||
|
||||
def cmd_web(self, args):
|
||||
print(f"🌐 网页抓取: {args.url or '(交互模式)'}")
|
||||
if args.url:
|
||||
return self._run_script("content-inspiration/run.py",
|
||||
["web", args.url] +
|
||||
(["--output", args.output] if args.output else []))
|
||||
print("请提供 URL: agentos crawl web <url>")
|
||||
return 1
|
||||
|
||||
def cmd_video(self, args):
|
||||
print(f"🎥 视频下载: {args.url or '(交互模式)'}")
|
||||
if args.url:
|
||||
return self._run_script("content-inspiration/run.py",
|
||||
["video", args.url, "--format", args.format])
|
||||
print("请提供视频 URL: agentos crawl video <url>")
|
||||
return 1
|
||||
|
||||
def cmd_extract(self, args):
|
||||
print(f"🔍 信息提取: {args.file or '(交互模式)'}")
|
||||
if args.file:
|
||||
cmd = ["extract", args.file]
|
||||
if args.fields:
|
||||
cmd += ["--fields", args.fields]
|
||||
return self._run_script("content-inspiration/run.py", cmd)
|
||||
if args.url:
|
||||
return self._run_script("content-inspiration/run.py", ["extract", "--url", args.url])
|
||||
print("请指定文件或URL: agentos crawl extract <file> 或 --url <url>")
|
||||
return 1
|
||||
|
||||
def cmd_schedule(self, args):
|
||||
print(f"⏰ 采集定时任务 {args.action}")
|
||||
return self._run_script("content-inspiration/run.py", ["schedule", args.action])
|
||||
|
||||
def cmd_source(self, args):
|
||||
print(f"📡 采集源 {args.action}")
|
||||
cmd = ["source", args.action]
|
||||
if args.name:
|
||||
cmd += ["--name", args.name]
|
||||
if args.src_url:
|
||||
cmd += ["--url", args.src_url]
|
||||
if args.src_type:
|
||||
cmd += ["--type", args.src_type]
|
||||
return self._run_script("content-inspiration/run.py", cmd)
|
||||
|
||||
def cmd_inspiration(self, args):
|
||||
print(f"💡 内容灵感 {args.action}")
|
||||
cmd = ["inspiration", args.action]
|
||||
if args.keyword:
|
||||
cmd += ["--keyword", args.keyword]
|
||||
return self._run_script("content-inspiration/run.py", cmd)
|
||||
Reference in New Issue
Block a user