ythx-101/x-tweet-fetcher

GitHub: ythx-101/x-tweet-fetcher

无需Twitter API密钥即可抓取推文、时间线、回复和文章的Python工具,专为AI Agent设计。

Stars: 815 | Forks: 74

# 🦞 x-tweet-fetcher **获取推文、列表、文章和微信内容——支持智能后端路由。** [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE) [![OpenClaw Skill](https://img.shields.io/badge/OpenClaw-Skill-blue.svg)](https://github.com/openclaw/openclaw) [![Python 3.7+](https://img.shields.io/badge/Python-3.7+-green.svg)](https://www.python.org) [![GitHub stars](https://img.shields.io/github/stars/ythx-101/x-tweet-fetcher?style=social)](https://github.com/ythx-101/x-tweet-fetcher) *三种后端 · 自动回退 · 无处不在 (VPS / Mac / Windows / CI / Claude Code / OpenClaw)* [快速入门](#-quick-start) · [后端](#-three-backends) · [功能](#-capabilities) · [Agent Waystation](#-agent-waystation) · [自托管 Nitter](#-self-hosted-nitter-setup) · [Claude Code & CC](#-works-with-claude-code--cc)
## 😤 问题 ``` You: fetch that tweet / list / article for me AI: I can't access X/Twitter. Please copy-paste the content manually. You: ...seriously? ``` X 没有免费 API。抓取会被封禁。浏览器自动化不仅脆弱,而且在无头环境下无法运行。 **x-tweet-fetcher** 通过**智能后端路由**解决了这个问题:Nitter 提供零依赖的高速度,Playwright 提供完整功能覆盖,并在两者之间自动回退。 ## 🔀 三种后端 ``` # Auto 模式(默认) — Nitter 优先,浏览器回退 python3 scripts/fetch_tweet.py --user elonmusk # 仅 Nitter — 零依赖,无需浏览器 python3 scripts/fetch_tweet.py --user elonmusk --backend nitter # 仅浏览器 — 全功能(列表、文章) python3 scripts/fetch_tweet.py --list 1455045069516357634 --backend browser ``` | Backend | Deps | Speed | Features | |---------|------|-------|----------| | **nitter** | 无(仅标准库) | ⚡ 快 | 时间线、搜索、回复、个人资料、提及 | | **browser** | Playwright/Chromium | 🐢 较慢 | 包含上述所有 + **列表** + **文章** + **fetch_china** | | **auto**(默认) | 当前最佳可用 | ⚡→🐢 | 先尝试 nitter,失败后回退到浏览器 | ## 📊 功能 | Feature | Backend | Output | |---------|---------|--------| | 单条推文 | FxTwitter(始终) | 文本、统计数据、媒体、引用 | | 回复评论 | nitter / browser | 线程化评论列表 | | 用户时间线 | nitter / browser | 分页推文列表 | | @提及监控 | nitter / browser | 增量新提及 | | 关键词搜索 | nitter / browser | 实时推文流 | | **X 列表** | **仅限 browser** | 列表成员推文 | | **X 文章** | **仅限 browser** | 完整长文内容 | | 用户资料分析 | nitter + LLM | MBTI、大五人格、主题图谱 | | 微信文章搜索 | Sogou(直接 HTTP) | 标题、URL、作者、日期 | | **微信/微博/Bilibili** | **仅限 browser** | 通过 fetch_china.py | | 推文增长追踪器 | FxTwitter API | 增长曲线、爆发检测 | ## 🧭 Agent Waystation x-tweet-fetcher 是从 **[Agent Waystation](https://github.com/ythx-101/openclaw-qa)** 维护的实战工具之一 —— 这是一个为 AI agent、构建者、工具和实战报告设立的驿站。 如果你正在构建一个会阅读 X/Twitter 的 agent,把它带到 Waystation 来: - 你的 agent 观察什么? - 它记住什么? - 它在哪里失败? - 它还需要什么实战工具? 中文:如果你也在养一个会看 X/Twitter 的 Agent,欢迎带它来 **Agent 驿站** 报到。说说它会看什么、会记什么、最容易在哪翻车。 → **[访问 Agent Waystation / Agent 驿站](https://github.com/ythx-101/openclaw-qa)** ## 🚀 快速入门 ### 单条推文(零配置) ``` # 开箱即用 — 无需 Nitter,无需浏览器 python3 scripts/fetch_tweet.py --url https://x.com/elonmusk/status/123456789 ``` ### 时间线、搜索、回复 ``` # 设置你的 Nitter 实例 URL(用于 nitter/auto 模式) export NITTER_URL=http://127.0.0.1:8788 # 用户时间线 python3 scripts/fetch_tweet.py --user elonmusk --limit 20 # 关键词搜索 — 实时推文 python3 scripts/nitter_client.py --search "AI agent" # 推文回复 python3 scripts/fetch_tweet.py --url https://x.com/elonmusk/status/123456789 --replies # @mentions 监控(兼容 cron) python3 scripts/fetch_tweet.py --monitor @yourusername # 用户资料分析 python3 scripts/x-profile-analyzer.py --user elonmusk --count 100 ``` ### 列表与文章(browser 后端) ``` # X List — 需要 Playwright python3 scripts/fetch_tweet.py --list 1455045069516357634 --backend browser # X Article python3 scripts/fetch_tweet.py --article https://x.com/user/article/123 --backend browser # WeChat / Weibo / Bilibili python3 scripts/fetch_china.py --url "https://mp.weixin.qq.com/s/..." ``` ### 微信搜索(始终零依赖) ``` python3 scripts/sogou_wechat.py --keyword "AI Agent" --limit 5 --json ``` ## 🖥️ 与 Claude Code / CC 协同工作 由于 x-tweet-fetcher **无强制性依赖**,它在受限环境中能完美运行: | Environment | nitter mode | browser mode | Notes | |-------------|:----------:|:------------:|-------| | **Claude Code (CC)** | ✅ | ❌ | 无浏览器运行时 | | **OpenClaw** | ✅ | ✅ | 内置 Playwright | | **VPS(无头 Linux)** | ✅ | ✅* | *需要 `pip install playwright` | | **Mac / Windows** | ✅ | ✅* | *需要 `pip install playwright` | | **CI/CD 流水线** | ✅ | ⚠️ | 可行但较重 | | **Docker 容器** | ✅ | ⚠️ | 镜像中需要 Chromium | | **Termux (Android)** | ✅ | ❌ | 无 Chromium | ``` # 在 Claude Code 中(nitter 模式,零依赖): export NITTER_URL=http://your-vps:8788 python3 scripts/fetch_tweet.py --user YuLin807 --limit 10 # 在 OpenClaw 中(auto 模式,全功能): python3 scripts/fetch_tweet.py --user YuLin807 --limit 10 # → 自动检测 Nitter,必要时回退至 Playwright ``` ## 🔧 自托管 Nitter 设置 ### 为什么你需要它 Twitter 在 2023 年移除了访客 API 访问权限。公共 Nitter 实例因为成千上万的用户共享少数账号而受到速率限制。**你自己的实例 = 你自己的速率限制。** ### 5 分钟设置指南 #### 1. 安装依赖 ``` # Ubuntu/Debian sudo apt install -y redis-server libpcre3-dev libsass-dev # 安装 Nim curl https://nim-lang.org/choosenim/init.sh -sSf | sh export PATH=$HOME/.nimble/bin:$PATH ``` #### 2. 构建 Nitter ``` git clone https://github.com/zedeus/nitter cd nitter nimble build -d:release nimble scss cp nitter.example.conf nitter.conf ``` #### 3. 获取 X 会话 Cookie 使用**小号**(而不是主账号)。 1. 在浏览器中登录 X → 开发者工具 (DevTools) → 应用程序 (Application) → Cookies → `x.com` 2. 复制 `auth_token` 和 `ct0` 3. 创建 `sessions.jsonl`: ``` {"name":"myaccount","auth_token":"YOUR_AUTH_TOKEN","ct0":"YOUR_CT0"} ``` #### 4. 配置 ``` [Server] address = "127.0.0.1" # Local only! port = 8788 [Config] hmacKey = "$(openssl rand -hex 32)" [Tokens] tokenFile = "sessions.jsonl" ``` #### 5. 运行与测试 ``` sudo systemctl start redis-server ./nitter # 测试 curl http://127.0.0.1:8788/YuLin807 export NITTER_URL=http://127.0.0.1:8788 python3 scripts/nitter_client.py --search "test" ``` ### 安全性 - **仅绑定到 `127.0.0.1`** —— 切勿暴露在公共互联网上 - **使用 X 小号** —— 会话 token 拥有完全访问权限 - **会话 token 有效期约 1 年** ## 📐 工作原理 ``` ┌─────────────┐ --url │ FxTwitter │ ← Public API, no auth needed │ (free) │ └──────┬──────┘ │ JSON ┌────────────┴────────────┐ │ --backend auto │ │ ┌───────┐ ┌────────┐ │ ┌──────────┐ --user │ │Nitter │→→│Browser │ │ │ Agent │ --replies │ │(fast) │ │(full) │ │──────▶│ (JSON) │ --monitor │ │ 0 dep │ │Playwrt │ │ │ │ --search │ └───────┘ └────────┘ │ └──────────┘ --list └─────────────────────────┘ --article ┌─────────────┐ sogou_wechat │ Sogou │ ← Direct HTTP, no API key fetch_china │ (search) │ └─────────────┘ ``` - **单条推文**:[FxTwitter](https://github.com/FxEmbed/FxEmbed) —— 始终有效,零授权 - **时间线 / 回复 / 搜索 / 提及**:自托管 [Nitter](https://github.com/zedeus/nitter) 或 Playwright 浏览器 - **列表 / 文章**:Playwright 浏览器(Nitter 不支持这些功能) - **微信 / 中国平台**:Sogou 搜索 + fetch_china.py ## 📦 依赖要求 ``` Python 3.7+ (that's it for nitter mode) ``` | Mode | Extra requirement | |------|-----------------| | `--backend nitter` | 无(仅 Python 标准库) | | `--backend browser` | `pip install playwright` + `playwright install chromium` | | `--backend auto` | 使用当前可用的任何模式 | ## ⏰ Cron 集成 用于自动化的退出代码:`0`=无新内容,`1`=有新内容,`2`=发生错误。 ``` # 每 30 分钟检查 mentions */30 * * * * NITTER_URL=http://127.0.0.1:8788 python3 fetch_tweet.py --monitor @username # 每天发现推文 0 9 * * * python3 nitter_client.py --search "AI Agent" >> ~/discoveries.jsonl ``` ## 🤝 参与贡献 欢迎提交 Issue 和 PR!核心平台: - **X/Twitter** —— Nitter + Playwright 后端 - **微信文章** —— Sogou 搜索 其他平台欢迎以社区 PR 的形式提交。 ## 🙏 致谢 - **[Nitter](https://github.com/zedeus/nitter)**,作者 [zedeus](https://github.com/zedeus) (12.6k ⭐) —— 自托管 Twitter 前端 - **[FxTwitter](https://github.com/FxEmbed/FxEmbed)** —— 用于获取单条推文数据的公共 API - **[Playwright](https://github.com/microsoft/playwright)** —— 提供完整功能覆盖的浏览器自动化工具 - **[OpenClaw](https://github.com/openclaw/openclaw)** —— AI agent 框架 ## 📄 许可证 [MIT](LICENSE)
*三种后端。自动回退。无处不在。* 🦞 **[GitHub](https://github.com/ythx-101/x-tweet-fetcher)** · **[Issues](https://github.com/ythx-101/x-tweet-fetcher/issues)** · **[OpenClaw Q&A](https://github.com/ythx-101/openclaw-qa)**
标签:AI智能体, Claude Code, ESC4, Nitter, OSINT, Playwright, Python, VPS, X/Twitter数据抓取, 免登录, 后端路由, 命令控制, 微信内容获取, 推文获取, 数字取证, 数据泄露, 数据采集, 无API密钥, 无后门, 无头浏览器, 智能降级, 特征检测, 社交媒体监控, 自动化脚本, 逆向工具