refactor(kb-zhihu-metadata): Simplify to only support zvideo and zhuanlan
- Remove question, answer, user types (not needed for knowledge base) - Keep only zvideo (知乎视频) and zhuanlan (知乎专栏文章) - Update URL patterns and extraction logic - Simplified CLI interface
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
"""
|
||||
知乎元数据获取工具
|
||||
|
||||
支持获取知乎问题、回答、用户的元数据信息。
|
||||
仅支持两种内容类型:
|
||||
- zvideo: 知乎视频 (https://www.zhihu.com/zvideo/xxx)
|
||||
- zhuanlan: 知乎专栏文章 (https://zhuanlan.zhihu.com/p/xxx)
|
||||
"""
|
||||
|
||||
import argparse
|
||||
@@ -19,52 +21,34 @@ if current_dir not in sys.path:
|
||||
from zhihu_extractor import ZhihuPlaywrightExtractor
|
||||
|
||||
|
||||
def extract_question(question_id: str, headless: bool = True) -> Dict[str, Any]:
|
||||
def extract_zvideo(video_id: str, headless: bool = True) -> Dict[str, Any]:
|
||||
"""
|
||||
获取知乎问题元数据
|
||||
获取知乎视频元数据
|
||||
|
||||
Args:
|
||||
question_id: 问题 ID (数字字符串)
|
||||
video_id: 视频 ID (数字字符串)
|
||||
headless: 是否使用无头浏览器
|
||||
|
||||
Returns:
|
||||
问题元数据字典
|
||||
视频元数据字典
|
||||
"""
|
||||
url = f"https://www.zhihu.com/question/{question_id}"
|
||||
return _extract_metadata(url, "question", headless)
|
||||
url = f"https://www.zhihu.com/zvideo/{video_id}"
|
||||
return _extract_metadata(url, "zvideo", headless)
|
||||
|
||||
|
||||
def extract_answer(answer_id: str, headless: bool = True) -> Dict[str, Any]:
|
||||
def extract_zhuanlan(article_id: str, headless: bool = True) -> Dict[str, Any]:
|
||||
"""
|
||||
获取知乎回答元数据
|
||||
获取知乎专栏文章元数据
|
||||
|
||||
Args:
|
||||
answer_id: 回答 ID (数字字符串)
|
||||
article_id: 文章 ID (数字字符串)
|
||||
headless: 是否使用无头浏览器
|
||||
|
||||
Returns:
|
||||
回答元数据字典
|
||||
文章元数据字典
|
||||
"""
|
||||
# 回答 URL 格式: https://www.zhihu.com/question/{question_id}/answer/{answer_id}
|
||||
# 需要先获取问题ID,这里简化处理,尝试从回答ID构建URL
|
||||
# 实际上知乎回答URL需要问题ID,这里用模拟URL
|
||||
url = f"https://www.zhihu.com/question/0/answer/{answer_id}"
|
||||
return _extract_metadata(url, "answer", headless)
|
||||
|
||||
|
||||
def extract_user(url_token: str, headless: bool = True) -> Dict[str, Any]:
|
||||
"""
|
||||
获取知乎用户信息
|
||||
|
||||
Args:
|
||||
url_token: 用户 URL token
|
||||
headless: 是否使用无头浏览器
|
||||
|
||||
Returns:
|
||||
用户信息字典
|
||||
"""
|
||||
url = f"https://www.zhihu.com/people/{url_token}"
|
||||
return _extract_user_metadata(url, headless)
|
||||
url = f"https://zhuanlan.zhihu.com/p/{article_id}"
|
||||
return _extract_metadata(url, "zhuanlan", headless)
|
||||
|
||||
|
||||
def _extract_metadata(url: str, content_type: str, headless: bool) -> Dict[str, Any]:
|
||||
@@ -73,7 +57,7 @@ def _extract_metadata(url: str, content_type: str, headless: bool) -> Dict[str,
|
||||
|
||||
Args:
|
||||
url: 知乎 URL
|
||||
content_type: 内容类型 (question/answer)
|
||||
content_type: 内容类型 (zvideo/zhuanlan)
|
||||
headless: 是否使用无头浏览器
|
||||
|
||||
Returns:
|
||||
@@ -104,120 +88,33 @@ def _extract_metadata(url: str, content_type: str, headless: bool) -> Dict[str,
|
||||
}
|
||||
|
||||
|
||||
def _extract_user_metadata(url: str, headless: bool) -> Dict[str, Any]:
|
||||
"""
|
||||
内部函数:提取用户信息
|
||||
|
||||
Args:
|
||||
url: 用户主页 URL
|
||||
headless: 是否使用无头浏览器
|
||||
|
||||
Returns:
|
||||
用户信息字典
|
||||
"""
|
||||
extractor = ZhihuPlaywrightExtractor(headless=headless)
|
||||
|
||||
try:
|
||||
extractor.browser.start()
|
||||
extractor.browser.goto(url, wait_until="networkidle")
|
||||
|
||||
import time
|
||||
time.sleep(3)
|
||||
|
||||
# 提取用户名
|
||||
username = ""
|
||||
username_elem = extractor.browser.query_selector('.ProfileHeader-name')
|
||||
if username_elem:
|
||||
username = username_elem.inner_text()
|
||||
|
||||
# 提取个人简介
|
||||
headline = ""
|
||||
headline_elem = extractor.browser.query_selector('.ProfileHeader-headline')
|
||||
if headline_elem:
|
||||
headline = headline_elem.inner_text()
|
||||
|
||||
# 提取简介详情
|
||||
description = ""
|
||||
desc_elem = extractor.browser.query_selector('.ProfileHeader-description')
|
||||
if desc_elem:
|
||||
description = desc_elem.inner_text()
|
||||
|
||||
# 提取关注者数
|
||||
followers = 0
|
||||
follower_elem = extractor.browser.query_selector('.FollowshipCard-followersNum')
|
||||
if follower_elem:
|
||||
follower_text = follower_elem.inner_text()
|
||||
try:
|
||||
followers = int(follower_text.replace(',', ''))
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
# 提取关注者数
|
||||
following = 0
|
||||
following_elem = extractor.browser.query_selector('.FollowshipCard-followingsNum')
|
||||
if following_elem:
|
||||
following_text = following_elem.inner_text()
|
||||
try:
|
||||
following = int(following_text.replace(',', ''))
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
# 提取回答数
|
||||
answers = 0
|
||||
answers_elem = extractor.browser.query_selector('.Profile-mainColumn .Tabs-item span')
|
||||
if answers_elem:
|
||||
answers_text = answers_elem.inner_text()
|
||||
try:
|
||||
answers = int(answers_text.replace(',', ''))
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
metadata = {
|
||||
"type": "user",
|
||||
"url": url,
|
||||
"username": username,
|
||||
"headline": headline,
|
||||
"description": description,
|
||||
"followers": followers,
|
||||
"following": following,
|
||||
"answers_count": answers,
|
||||
}
|
||||
return metadata
|
||||
|
||||
except Exception as e:
|
||||
return {
|
||||
"type": "user",
|
||||
"error": str(e),
|
||||
"url": url
|
||||
}
|
||||
finally:
|
||||
extractor.browser.close()
|
||||
|
||||
|
||||
def main():
|
||||
"""命令行入口"""
|
||||
parser = argparse.ArgumentParser(
|
||||
description="知乎元数据获取工具",
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
epilog="""
|
||||
支持的类型:
|
||||
zvideo - 知乎视频 (https://www.zhihu.com/zvideo/123456)
|
||||
zhuanlan - 知乎专栏文章 (https://zhuanlan.zhihu.com/p/123456)
|
||||
|
||||
示例:
|
||||
python zhihu_metadata.py --id "266309957" --type question
|
||||
python zhihu_metadata.py --id "2003427891925509507" --type answer
|
||||
python zhihu_metadata.py --id "example-user" --type user
|
||||
python zhihu_metadata.py --id "123456" --type zvideo
|
||||
python zhihu_metadata.py --id "123456" --type zhuanlan
|
||||
"""
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--id", "-i",
|
||||
required=True,
|
||||
help="知乎内容 ID (问题ID、回答ID或用户URL token)"
|
||||
help="知乎内容 ID (视频ID或文章ID)"
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--type", "-t",
|
||||
required=True,
|
||||
choices=["question", "answer", "user"],
|
||||
help="内容类型"
|
||||
choices=["zvideo", "zhuanlan"],
|
||||
help="内容类型: zvideo=视频, zhuanlan=专栏文章"
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
@@ -236,12 +133,10 @@ def main():
|
||||
args = parser.parse_args()
|
||||
|
||||
# 提取元数据
|
||||
if args.type == "question":
|
||||
result = extract_question(args.id, headless=args.headless)
|
||||
elif args.type == "answer":
|
||||
result = extract_answer(args.id, headless=args.headless)
|
||||
else: # user
|
||||
result = extract_user(args.id, headless=args.headless)
|
||||
if args.type == "zvideo":
|
||||
result = extract_zvideo(args.id, headless=args.headless)
|
||||
else: # zhuanlan
|
||||
result = extract_zhuanlan(args.id, headless=args.headless)
|
||||
|
||||
# 输出结果
|
||||
json_output = json.dumps(result, ensure_ascii=False, indent=2)
|
||||
|
||||
Reference in New Issue
Block a user