weibo-hub
Adapted from: jackwener/weibo-cli (Apache-2.0)
This skill streamlines the original repository as follows:
- Removed the
click / rich / browser-cookie3 / qrcode / pyyaml dependencies
- Kept only
httpx as a third-party dependency
- Removed the CLI layer; all functionality is encapsulated as a synchronous Python API
- Changed authentication to extracting cookies with
browser_use get_cookies, then calling setup_credential() to save them
- Changed the data directory to
/var/minis/workspace/weibo-hub/
File Structure
/var/minis/skills/weibo-hub/
├── SKILL.md
├── pyproject.toml # httpx only
└── scripts/
├── __init__.py
├── constants.py # API endpoints, headers, and path constants
├── exceptions.py # WeiboError exception hierarchy
├── auth.py # Credential persistence (no browser-cookie3)
└── client.py # WeiboClient core class (all APIs)
Authentication Flow (Check Before Each Use)
weibo-hub uses browser cookie authentication, extracted from weibo.com with browser_use get_cookies.
It does not require browser-cookie3 or QR code scanning.
Step 1: Use browser_use to Extract Cookies
# Call in the agent:
browser_use(action="navigate", url="https://weibo.com")
# After confirming you are logged in:
browser_use(action="get_cookies", url="https://weibo.com")
# Record the returned offload env file path
Step 2: Read and Save Credentials from the env File
import sys, os, subprocess, json
# Load Cookie environment variables (the path comes from the offload file returned by get_cookies)
env_file = "/var/minis/offloads/env_cookies_xxx.sh" # Replace with the actual path
result = subprocess.run(
f". {env_file} && python3 -c \"import os,json; print(json.dumps(dict(os.environ)))\"",
shell=True, capture_output=True, text=True
)
env = json.loads(result.stdout)
# Parse the Cookie dictionary (variables with the COOKIE_ prefix)
cookies = {
k[len("COOKIE_"):]: v
for k, v in env.items()
if k.startswith("COOKIE_")
}
# Save credentials
sys.path.insert(0, "/var/minis/skills/weibo-hub")
from scripts.client import WeiboClient
WeiboClient.setup_credential(cookies)
Required Cookies: SUB, SUBP (required). The more additional cookies, the better.
Credentials are saved to /var/minis/workspace/weibo-hub/credential.json, valid for 7 days, and an expiration prompt is shown automatically when they expire.
Environment Setup
cd /var/minis/skills/weibo-hub
uv sync
Quick Start
import sys
sys.path.insert(0, "/var/minis/skills/weibo-hub")
from scripts.client import WeiboClient
with WeiboClient() as client:
# ── Trending searches / trends (no login required)──────────────────────────────
topics = client.hot_search() # Trending search list (~52 entries)
for t in topics[:10]:
print(f"#{t.get('realtime_hot_show_label','')} {t.get('word','')}")
band = client.hot_band() # Full trending search list
trends = client.trending() # Real-time search suggestions
# ── Feeds (popular requires no login; following requires login)─────────────────────
hot = client.hot_feed(count=10) # Popular timeline
home = client.home_feed(count=20) # Following timeline
# ── Search (mobile API)────────────────────────────────────
results = client.search("Artificial Intelligence", page=1)
for w in results[:5]:
print(w.get("text", "")[:80])
# ── Weibo post details / comments / reposts (login required)──────────────────
wb = client.detail("Qw06Kd98p")
cmt = client.comments("WeiboID", count=20)
rep = client.reposts("WeiboID", count=10)
# ── Users (login required)─────────────────────────────────────
me = client.me() # Currently logged-in user
user = client.profile("1699432410") # Specified user profile
weibos = client.user_weibos("1699432410", page=1)
following = client.following("1699432410", page=1)
followers = client.followers("1699432410", page=1)
API Quick Reference
No Login Required (Public APIs)
| Method |
Description |
hot_search() |
Trending search sidebar (~52 entries) |
hot_band() |
Full trending search list |
trending() |
Real-time search suggestions |
hot_feed(count, max_id) |
Popular timeline |
search(keyword, page) |
Keyword search for Weibo posts |
Login Required (Cookie Authentication)
| Method |
Description |
me() |
Current logged-in user information |
home_feed(count, max_id) |
Following timeline |
detail(mblogid) |
Details for a single Weibo post |
comments(weibo_id, count, max_id) |
Weibo comment list |
reposts(weibo_id, page, count) |
Weibo repost list |
profile(uid) |
User profile |
user_weibos(uid, page, count) |
User's Weibo post list |
following(uid, page) |
User following list |
followers(uid, page) |
User follower list |
Authentication
| Method |
Description |
WeiboClient.setup_credential(cookies) |
Save Cookie credentials (static method) |
Anti-Abuse Notes
Consistent with the upstream weibo-cli:
- Gaussian jitter: Interval between requests =
request_delay + gauss(0.3, 0.15), about 1 s
- 5% long pause: Randomly triggers an additional 2-5 s delay to simulate reading behavior
- Exponential backoff: HTTP 429/5xx retries up to 3 times, with a wait time of 2^n seconds
- Chrome 145 UA: Desktop User-Agent, consistent with the browser fingerprint
Notes
- Before first use, you must call
WeiboClient.setup_credential(cookies) to save credentials
- Credential file:
/var/minis/workspace/weibo-hub/credential.json, permissions 0600
- Required Cookies:
SUB + SUBP; if missing, setup_credential() throws ValueError
- Cookies automatically prompt as expired after 7 days and must be extracted again
- Trending searches, popular feeds, and search do not require login;
profile, detail, home_feed, and similar methods require valid cookies
search() uses the mobile API (m.weibo.cn), and the result format is slightly different
1---2name: weibo-hub3description: A skill for reading and writing Weibo data with Python + UV. It depends only on httpx and uses `browser_use get_cookies` to automatically retrieve cookies and complete authentication, with no manual copying required. It supports trending searches, popular feeds, following feeds, keyword search, Weibo post details/comments/reposts, user profiles/posts/following/follower lists, and more. This skill must be triggered whenever the user mentions "Weibo", "weibo", "weibo-hub", "Weibo trending searches", "scraping Weibo", "searching Weibo", "Weibo comments", "Weibo users", or any scenario that requires programmatic reading or writing of Weibo data.4---56# weibo-hub78> **Adapted from**: [jackwener/weibo-cli](https://github.com/jackwener/weibo-cli) (Apache-2.0)9>10> This skill streamlines the original repository as follows:11> - **Removed** the `click` / `rich` / `browser-cookie3` / `qrcode` / `pyyaml` dependencies12> - **Kept only** `httpx` as a third-party dependency13> - **Removed** the CLI layer; all functionality is encapsulated as a synchronous Python API14> - **Changed authentication to** extracting cookies with `browser_use get_cookies`, then calling `setup_credential()` to save them15> - Changed the data directory to `/var/minis/workspace/weibo-hub/`1617---1819## File Structure2021```22/var/minis/skills/weibo-hub/23├── SKILL.md24├── pyproject.toml # httpx only25└── scripts/26 ├── __init__.py27 ├── constants.py # API endpoints, headers, and path constants28 ├── exceptions.py # WeiboError exception hierarchy29 ├── auth.py # Credential persistence (no browser-cookie3)30 └── client.py # WeiboClient core class (all APIs)31```3233---3435## Authentication Flow (Check Before Each Use)3637weibo-hub uses **browser cookie authentication**, extracted from weibo.com with `browser_use get_cookies`.38It does not require `browser-cookie3` or QR code scanning.3940### Step 1: Use `browser_use` to Extract Cookies4142```python43# Call in the agent:44browser_use(action="navigate", url="https://weibo.com")45# After confirming you are logged in:46browser_use(action="get_cookies", url="https://weibo.com")47# Record the returned offload env file path48```4950### Step 2: Read and Save Credentials from the env File5152```python53import sys, os, subprocess, json5455# Load Cookie environment variables (the path comes from the offload file returned by get_cookies)56env_file = "/var/minis/offloads/env_cookies_xxx.sh" # Replace with the actual path57result = subprocess.run(58 f". {env_file} && python3 -c \"import os,json; print(json.dumps(dict(os.environ)))\"",59 shell=True, capture_output=True, text=True60)61env = json.loads(result.stdout)6263# Parse the Cookie dictionary (variables with the COOKIE_ prefix)64cookies = {65 k[len("COOKIE_"):]: v66 for k, v in env.items()67 if k.startswith("COOKIE_")68}6970# Save credentials71sys.path.insert(0, "/var/minis/skills/weibo-hub")72from scripts.client import WeiboClient73WeiboClient.setup_credential(cookies)74```7576> **Required Cookies**: `SUB`, `SUBP` (required). The more additional cookies, the better.77> Credentials are saved to `/var/minis/workspace/weibo-hub/credential.json`, valid for 7 days, and an expiration prompt is shown automatically when they expire.7879---8081## Environment Setup8283```bash84cd /var/minis/skills/weibo-hub85uv sync86```8788---8990## Quick Start9192```python93import sys94sys.path.insert(0, "/var/minis/skills/weibo-hub")95from scripts.client import WeiboClient9697with WeiboClient() as client:9899 # ── Trending searches / trends (no login required)──────────────────────────────100 topics = client.hot_search() # Trending search list (~52 entries)101 for t in topics[:10]:102 print(f"#{t.get('realtime_hot_show_label','')} {t.get('word','')}")103104 band = client.hot_band() # Full trending search list105 trends = client.trending() # Real-time search suggestions106107 # ── Feeds (popular requires no login; following requires login)─────────────────────108 hot = client.hot_feed(count=10) # Popular timeline109 home = client.home_feed(count=20) # Following timeline110111 # ── Search (mobile API)────────────────────────────────────112 results = client.search("Artificial Intelligence", page=1)113 for w in results[:5]:114 print(w.get("text", "")[:80])115116 # ── Weibo post details / comments / reposts (login required)──────────────────117 wb = client.detail("Qw06Kd98p")118 cmt = client.comments("WeiboID", count=20)119 rep = client.reposts("WeiboID", count=10)120121 # ── Users (login required)─────────────────────────────────────122 me = client.me() # Currently logged-in user123 user = client.profile("1699432410") # Specified user profile124 weibos = client.user_weibos("1699432410", page=1)125 following = client.following("1699432410", page=1)126 followers = client.followers("1699432410", page=1)127```128129---130131## API Quick Reference132133### No Login Required (Public APIs)134135| Method | Description |136|------|------|137| `hot_search()` | Trending search sidebar (~52 entries) |138| `hot_band()` | Full trending search list |139| `trending()` | Real-time search suggestions |140| `hot_feed(count, max_id)` | Popular timeline |141| `search(keyword, page)` | Keyword search for Weibo posts |142143### Login Required (Cookie Authentication)144145| Method | Description |146|------|------|147| `me()` | Current logged-in user information |148| `home_feed(count, max_id)` | Following timeline |149| `detail(mblogid)` | Details for a single Weibo post |150| `comments(weibo_id, count, max_id)` | Weibo comment list |151| `reposts(weibo_id, page, count)` | Weibo repost list |152| `profile(uid)` | User profile |153| `user_weibos(uid, page, count)` | User's Weibo post list |154| `following(uid, page)` | User following list |155| `followers(uid, page)` | User follower list |156157### Authentication158159| Method | Description |160|------|------|161| `WeiboClient.setup_credential(cookies)` | Save Cookie credentials (static method) |162163---164165## Anti-Abuse Notes166167Consistent with the upstream weibo-cli:168- **Gaussian jitter**: Interval between requests = `request_delay + gauss(0.3, 0.15)`, about 1 s169- **5% long pause**: Randomly triggers an additional 2-5 s delay to simulate reading behavior170- **Exponential backoff**: HTTP 429/5xx retries up to 3 times, with a wait time of 2^n seconds171- **Chrome 145 UA**: Desktop User-Agent, consistent with the browser fingerprint172173---174175## Notes176177- Before first use, you must call `WeiboClient.setup_credential(cookies)` to save credentials178- Credential file: `/var/minis/workspace/weibo-hub/credential.json`, permissions 0600179- Required Cookies: `SUB` + `SUBP`; if missing, `setup_credential()` throws `ValueError`180- Cookies automatically prompt as expired after 7 days and must be extracted again181- Trending searches, popular feeds, and search do not require login; `profile`, `detail`, `home_feed`, and similar methods require valid cookies182- `search()` uses the mobile API (`m.weibo.cn`), and the result format is slightly different