huibao/backend/debug_002273.py

80 lines
2.8 KiB
Python
Raw Permalink Normal View History

"""
调试002273报告获取问题
"""
import asyncio
import sys
sys.path.insert(0, ".")
from app.services.cninfo_crawler import cninfo_service
from app.database import AsyncSessionLocal, init_db
from app.models import Company, Report
from sqlalchemy import select
async def debug_002273():
print("=" * 60)
print("调试 002273 (水晶光电) 报告获取")
print("=" * 60)
await init_db()
async with AsyncSessionLocal() as db:
# 查询公司信息
stmt = select(Company).where(Company.stock_code == "002273")
result = await db.execute(stmt)
company = result.scalar_one_or_none()
if not company:
print("✗ 未找到公司 002273")
return
print(f"公司: {company.company_name}")
print(f"简称: {company.short_name}")
print(f"行业: {company.industry}")
print(f"org_id: {company.org_id}")
print(f"is_active: {company.is_active}")
# 查询已有报告
stmt = select(Report).where(Report.company_id == company.id)
result = await db.execute(stmt)
existing_reports = result.scalars().all()
print(f"\n已有报告数: {len(existing_reports)}")
for r in existing_reports:
print(f" - {r.title} (下载:{r.is_downloaded}, 提取:{r.is_extracted})")
# 测试爬虫搜索
print("\n" + "-" * 40)
print("测试从巨潮搜索报告...")
# 先获取 org_id
if not company.org_id:
print("正在获取 org_id...")
org_id = await cninfo_service.get_org_id("002273")
print(f"获取到 org_id: {org_id}")
else:
org_id = company.org_id
print(f"使用已有 org_id: {org_id}")
# 搜索报告
reports = await cninfo_service.search_reports("002273", org_id)
print(f"\n搜索到 {len(reports)} 份报告:")
for r in reports:
print(f" - [{r['report_year']}] {r['title']}")
print(f" 类型: {r['report_type']}")
print(f" PDF: {r.get('pdf_url', 'None')}")
# 写入文件
with open("debug_002273_result.txt", "w", encoding="utf-8") as f:
f.write(f"公司: {company.company_name}\n")
f.write(f"org_id: {company.org_id}\n")
f.write(f"已有报告数: {len(existing_reports)}\n\n")
f.write(f"搜索到 {len(reports)} 份报告:\n")
for r in reports:
f.write(f" - [{r['report_year']}] {r['title']}\n")
f.write(f" 类型: {r['report_type']}\n")
f.write(f" PDF: {r.get('pdf_url', 'None')}\n\n")
print("\n结果已写入 debug_002273_result.txt")
if __name__ == "__main__":
asyncio.run(debug_002273())