api_manifest.md
4.72 KB
微博多榜 API 调用示例
一、API 端点验证
1.1 热搜榜 (type=hot)
请求
curl -s "https://m.weibo.cn/api/index/list?type=hot" \
-H "User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 Mobile/15E148 Safari/604.1" \
-H "Accept: application/json" \
-H "Referer: https://m.weibo.cn"
响应 (模拟)
{
"ok": 1,
"msg": "success",
"data": {
"realtime": [
{
"word": "示例热搜话题",
"word_scheme": "/hot/123456",
"num": 1234567,
"raw_hot": 1234567,
"rank": 1,
"note": "这是热搜摘要内容",
"flag": 1,
"label_name": "新"
},
{
"word": "第二个热搜",
"word_scheme": "/hot/234567",
"num": 987654,
"raw_hot": 987654,
"rank": 2,
"note": "第二个热搜的摘要",
"flag": 1,
"label_name": ""
}
],
"more": ""
}
}
1.2 文娱榜 (type=entertainment)
请求
curl -s "https://m.weibo.cn/api/index/list?type=entertainment" \
-H "User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 Mobile/15E148 Safari/604.1" \
-H "Accept: application/json" \
-H "Referer: https://m.weibo.cn"
1.3 生活榜 (type=life)
请求
curl -s "https://m.weibo.cn/api/index/list?type=life" \
-H "User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 Mobile/15E148 Safari/604.1" \
-H "Accept: application/json" \
-H "Referer: https://m.weibo.cn"
1.4 社会榜 (type=social)
请求
curl -s "https://m.weibo.cn/api/index/list?type=social" \
-H "User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 Mobile/15E148 Safari/604.1" \
-H "Accept: application/json" \
-H "Referer: https://m.weibo.cn"
1.5 我的热搜 (type=mine)
请求
curl -s "https://m.weibo.cn/api/index/list?type=mine" \
-H "User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 Mobile/15E148 Safari/604.1" \
-H "Accept: application/json" \
-H "Referer: https://m.weibo.cn"
二、Python 调用示例
import requests
import random
import time
USER_AGENTS = [
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 Mobile/15E148 Safari/604.1"
]
BOARD_TYPES = ["hot", "entertainment", "life", "social", "mine"]
def fetch_board(board_type: str) -> dict:
"""获取指定榜单数据"""
url = f"https://m.weibo.cn/api/index/list?type={board_type}"
headers = {
"User-Agent": random.choice(USER_AGENTS),
"Accept": "application/json",
"Referer": "https://m.weibo.cn"
}
response = requests.get(url, headers=headers, timeout=10)
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code}")
return {}
def fetch_all_boards():
"""获取所有榜单数据"""
results = {}
for board_type in BOARD_TYPES:
print(f"Fetching {board_type}...")
data = fetch_board(board_type)
results[board_type] = data
time.sleep(3) # 频率控制
return results
# 测试
if __name__ == "__main__":
boards = fetch_all_boards()
for board_type, data in boards.items():
print(f"{board_type}: ok={data.get('ok')}")
三、返回数据处理
3.1 数据提取
def extract_items(data: dict, board_type: str) -> list:
"""从 API 返回中提取热搜条目"""
if data.get("ok") != 1:
return []
realtime = data.get("data", {}).get("realtime", [])
items = []
for item in realtime:
if item.get("flag", 0) in [1, 2]:
items.append({
"board": board_type,
"word": item.get("word", ""),
"url": f"https://s.weibo.com/weibo?q={item.get('word', '')}",
"rank": item.get("rank", 0),
"hot": item.get("raw_hot", 0),
"note": item.get("note", ""),
"label": item.get("label_name", "")
})
return items
3.2 数据合并
def merge_boards(boards_data: dict) -> dict:
"""合并多榜数据"""
all_items = []
board_summary = {}
for board_type, data in boards_data.items():
items = extract_items(data, board_type)
board_summary[board_type] = {
"count": len(items),
"items": items
}
all_items.extend(items)
return {
"date": time.strftime("%Y-%m-%d"),
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ"),
"boards": board_summary,
"total": len(all_items)
}
生成时间: 2026-03-29