feat: 添加知乎问答支持

- zhihu_extractor.py:
  - 添加 URL_PATTERNS 支持 answer 和 question
  - 添加 _extract_answer_content() 方法提取知乎回答
  - 添加 _extract_question_content() 方法提取知乎问题

- zhihu_metadata.py:
  - 添加 extract_answer() 函数
  - 添加 extract_question() 函数
  - 更新命令行参数支持 answer/question 类型
  - 更新帮助文档
This commit is contained in:
2026-02-16 22:12:13 +08:00
parent 09618f52c5
commit d2c262973e
2 changed files with 165 additions and 5 deletions
+50 -5
View File
@@ -2,9 +2,11 @@
"""
知乎元数据获取工具
仅支持两种内容类型:
支持以下内容类型:
- zvideo: 知乎视频 (https://www.zhihu.com/zvideo/xxx)
- zhuanlan: 知乎专栏文章 (https://zhuanlan.zhihu.com/p/xxx)
- answer: 知乎回答 (https://www.zhihu.com/question/xxx/answer/yyy)
- question: 知乎问题 (https://www.zhihu.com/question/xxx)
"""
import argparse
@@ -51,6 +53,38 @@ def extract_zhuanlan(article_id: str, headless: bool = True) -> Dict[str, Any]:
return _extract_metadata(url, "zhuanlan", headless)
def extract_answer(answer_id: str, headless: bool = True) -> Dict[str, Any]:
"""
获取知乎回答元数据
Args:
answer_id: 回答 ID (数字字符串)
headless: 是否使用无头浏览器
Returns:
回答元数据字典
"""
# 回答需要构建包含问题ID的URL,但answer_id本身足以识别
# 使用一个占位符问题ID,因为提取器会从URL中提取answer_id
url = f"https://www.zhihu.com/question/0/answer/{answer_id}"
return _extract_metadata(url, "answer", headless)
def extract_question(question_id: str, headless: bool = True) -> Dict[str, Any]:
"""
获取知乎问题元数据
Args:
question_id: 问题 ID (数字字符串)
headless: 是否使用无头浏览器
Returns:
问题元数据字典
"""
url = f"https://www.zhihu.com/question/{question_id}"
return _extract_metadata(url, "question", headless)
def _extract_metadata(url: str, content_type: str, headless: bool) -> Dict[str, Any]:
"""
内部函数:提取元数据
@@ -97,24 +131,28 @@ def main():
支持的类型:
zvideo - 知乎视频 (https://www.zhihu.com/zvideo/123456)
zhuanlan - 知乎专栏文章 (https://zhuanlan.zhihu.com/p/123456)
answer - 知乎回答 (https://www.zhihu.com/question/xxx/answer/yyy)
question - 知乎问题 (https://www.zhihu.com/question/xxx)
示例:
python zhihu_metadata.py --id "123456" --type zvideo
python zhihu_metadata.py --id "123456" --type zhuanlan
python zhihu_metadata.py --id "123456" --type answer
python zhihu_metadata.py --id "123456" --type question
"""
)
parser.add_argument(
"--id", "-i",
required=True,
help="知乎内容 ID (视频ID或文章ID)"
help="知乎内容 ID (视频ID/文章ID/回答ID/问题ID)"
)
parser.add_argument(
"--type", "-t",
required=True,
choices=["zvideo", "zhuanlan"],
help="内容类型: zvideo=视频, zhuanlan=专栏文章"
choices=["zvideo", "zhuanlan", "answer", "question"],
help="内容类型: zvideo=视频, zhuanlan=专栏文章, answer=回答, question=问题"
)
parser.add_argument(
@@ -135,8 +173,15 @@ def main():
# 提取元数据
if args.type == "zvideo":
result = extract_zvideo(args.id, headless=args.headless)
else: # zhuanlan
elif args.type == "zhuanlan":
result = extract_zhuanlan(args.id, headless=args.headless)
elif args.type == "answer":
result = extract_answer(args.id, headless=args.headless)
elif args.type == "question":
result = extract_question(args.id, headless=args.headless)
else:
print(f"不支持的类型: {args.type}")
sys.exit(1)
# 输出结果
json_output = json.dumps(result, ensure_ascii=False, indent=2)