Files
schoolNews/schoolNewsCrawler/crawler/rmrb/RmrbHotPoint.py

82 lines
2.2 KiB
Python
Raw Normal View History

2025-11-10 19:13:54 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
人民日报热点排行爬虫命令行工具
用法: python RmrbHotPoint.py
"""
import argparse
import json
import sys
from pathlib import Path
# Add parent directory to path to import crawler
2025-11-19 16:41:41 +08:00
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
2025-11-10 19:13:54 +08:00
2025-11-19 16:04:50 +08:00
from crawler.rmrb.RmrbCrawler import RmrbCrawler
2025-11-10 19:13:54 +08:00
from loguru import logger
def main():
"""主函数"""
parser = argparse.ArgumentParser(
description='人民日报热点排行获取工具',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
示例:
python RmrbHotPoint.py
2025-11-12 16:10:34 +08:00
python RmrbHotPoint.py --output "output/hotpoint.json"
2025-11-10 19:13:54 +08:00
"""
)
2025-11-12 16:10:34 +08:00
# 添加输出文件参数
parser.add_argument(
'--output', '-o',
type=str,
help='输出文件路径'
)
2025-11-10 19:13:54 +08:00
args = parser.parse_args()
2025-11-12 16:10:34 +08:00
output_file = args.output
logger.info("使用直接参数模式")
2025-11-10 19:13:54 +08:00
try:
logger.info("开始获取人民日报热点排行")
crawler = RmrbCrawler()
result = crawler.hotPointRank()
output = {
"code": result.code,
"message": result.message,
"success": result.success,
"data": None,
2025-11-12 19:16:50 +08:00
"dataList": [item.model_dump() for item in result.dataList] if result.dataList else []
2025-11-10 19:13:54 +08:00
}
2025-11-12 16:10:34 +08:00
if output_file:
output_path = Path(output_file)
output_path.parent.mkdir(parents=True, exist_ok=True)
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(output, f, ensure_ascii=False, indent=2)
logger.info(f"结果已保存到: {output_file}")
2025-11-10 19:13:54 +08:00
2025-11-12 16:10:34 +08:00
print(json.dumps(output, ensure_ascii=False, indent=2))
2025-11-10 19:13:54 +08:00
crawler.close()
sys.exit(0 if result.success else 1)
except Exception as e:
logger.error(f"执行失败: {str(e)}")
error_output = {
"code": 500,
"message": f"执行失败: {str(e)}",
"success": False,
"data": None,
"dataList": []
}
print(json.dumps(error_output, ensure_ascii=False, indent=2))
sys.exit(1)
2025-11-12 16:10:34 +08:00
2025-11-10 19:13:54 +08:00
if __name__ == "__main__":
2025-11-12 16:10:34 +08:00
main()