Files
pytdxdata/examples/server_files.py
T
openbot-coder 9183c3294f v0.4.0: 初始开源发布
通达信(TDX)行情数据客户端 —— 动态连接池 + 双层TTL缓存 + 3连接并发分页,
纯 asyncio、零运行时依赖。一套接口覆盖 A股(沪/深/北) / 港股 / 美股 / 期货 / 期权。

主要能力:
- 四层架构:api → pool(标准/MAC/EX 三池) → protocol(16 命令模块) → cache(内存+SQLite)
- TdxData 提供 51 个公开异步方法;CLI(tdx) 提供 19 个子命令
- 服务器文件下载 + 解析:板块 block_*.dat / 行业 tdxhy.cfg / 历史财报 gpcw*.zip
- 85 个离线测试(协议 / 连接池 / 缓存 / 分页 / API / 文件解析 / CLI)

协议层参照 easy-tdx 的字节级格式独立实现,不依赖 easy-tdx 包。
2026-09-15 11:38:44 +08:00

51 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""服务器文件下载 + 解析:板块 / 行业分类 / 历史专业财报。
运行:
python examples/server_files.py
这组接口把通达信服务器上的二进制文件(block_*.dat / tdxhy.cfg / gpcw*.zip)
直接下载并解析为结构化数据(解析器位于 pytdxdata.codec,纯标准库、可脱网单测)。
"""
import asyncio
from pytdxdata import TdxData
async def main() -> None:
async with TdxData() as td:
# 1) 板块文件 -> 板块成分
# block_zs.dat=行业 / block_gn.dat=概念 / block_fg.dat=风格
blocks = await td.get_block_parsed("block_gn.dat")
print(f"== 概念板块(共 {len(blocks)} 个)==")
for blk in blocks[:5]:
print(f" [{blk.category}] {blk.name} 成分数={blk.count} 示例={blk.codes[:5]}")
# 2) 行业分类(tdxhy.cfg)-> {6位代码: 通达信行业 + 申万行业}
industry = await td.get_industry_map()
print(f"\n== 行业分类(共 {len(industry)} 只)==")
for code in ("600000", "000001", "300750"):
info = industry.get(code)
if info is not None:
sw = info.sw_industry or "-"
print(f" {code} 通达信={info.tdx_industry} 申万={sw}")
# 3) 历史专业财报文件索引(tdxfin/gpcw.txt)
infos = await td.get_financial_file_infos()
print(f"\n== 财报文件索引(共 {len(infos)} 个)==")
for fi in infos[-3:]:
print(f" {fi.filename} md5={fi.hash[:12]}... {fi.filesize} 字节")
# 4) 下载并解析最新一期财报(gpcw*.zip -> 每股原始 float 字段)
if infos:
latest = infos[-1].filename
records = await td.get_financial_records_parsed(latest)
print(f"\n== {latest} 财报记录(共 {len(records)} 条)==")
for r in records[:5]:
market = "沪" if r.market == 1 else "深"
print(f" {r.code} 市场={market} 报告期={r.report_date} 字段数={len(r.fields)}")
if __name__ == "__main__":
asyncio.run(main())