Skip to main content

fetch data from jupyter notebook

Project description

jupyter-data-fetch

jupyterlabjupyter notebook中抓取数据的示例

优点

  1. ksrpc比,通用性更强,理论上全平台通用
  2. 不需中转服务器,网页能打开就能使用

缺点

  1. ksrpc传输是二进制,而本项目编码成了base85/base64,速度较慢
  2. 传输带宽消耗多,base64多占33%,base85多占25%

安装

  1. uv pip install jupyter-data-fetch -U

使用方法

  1. examples下提供了示例
  2. joinquant为例,打开浏览器,登录研究环境,按F12打开开发者工具
  3. 搜索kernels,复制请求URLCookie devtool.png
  4. 替换示例中Cookieserver_url即可 ide.png
  5. 留意:server_url只复制一段。Cookie要完整复制

最简示例

from jupyter_kernel_client import KernelClient

from jupyter_data_fetch.codec import JupyterTextCodec

# ... 省去部分代码。更多参考examples/joinquant.py

with KernelClient(server_url="https://www.joinquant.com/user/12345678901", token=None, headers=headers) as kernel:
    # 一定要保证缩进正确
    code = """
df = get_fundamentals(query(
        valuation, income
    ).filter(
        # 这里不能使用 in 操作, 要使用in_()函数
        valuation.code.in_(['000001.XSHE', '600000.XSHG'])
    ), date='2015-10-15')
"""
    reply = kernel.execute(JupyterTextCodec.generate_code(code, var_name='df'), store_history=False)
    # print(reply)
    obj = JupyterTextCodec.extract_decode(reply)
    print(obj)

进阶函数

  1. 由于code是字符串,动态传入list/dict太麻烦,所以还提供了auto_execute
  2. 用户提前对函数套上auto_execute装饰器,就可以快速使用
  3. 参考examples/jqresearch.py

自动登录并获取数据的完整示例

参考examples/playwright/joinquant.py

核心代码

  1. JupyterTextCodec: 目前使用base85编解码器,使用字符串传输数据,压缩率高。字符串被截断时,必须使用JupyterImageCodec
  2. JupyterImageCodec: 图片编解码器,使用图片传输数据,base64编码压缩率低
  3. generate_code生成可在Notebook单元格中运行的代码字符串,一定要指定需要获取的变量名var_name
  4. kernel.execute在服务段执行字符串代码,返回json对象
  5. extract_decodejson中提取数据后解码成对象

注意

  1. 由于各平台限制,generate_code生成的代码可能无法运行,可以复制到Notebook中测试
  2. python3.6问题太多,可以打开一个ipynb文件后,通过菜单更改内核为最新版
  3. 可以连接到已经打开的内核,只要提供kernel_id参数即可。参考ricequant.py示例
  4. Notebook中可以导入当前目录中py,但本项目直接使用当前目录是/,导致导入失败,通过指定kernel_id可解决

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jupyter_data_fetch-0.1.2.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jupyter_data_fetch-0.1.2-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file jupyter_data_fetch-0.1.2.tar.gz.

File metadata

  • Download URL: jupyter_data_fetch-0.1.2.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for jupyter_data_fetch-0.1.2.tar.gz
Algorithm Hash digest
SHA256 229454f6267a8e8b9d81ac7955b885a006bb8697cc7095d318b26af1d387a0a8
MD5 bc4ef4cbf1c158bf8d6616cd843457d4
BLAKE2b-256 c483dca4a9840328edae0d8f166265cdddb201b24367898cceafe90849de148e

See more details on using hashes here.

File details

Details for the file jupyter_data_fetch-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for jupyter_data_fetch-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7eeefcbcfbc6d0edcb25d33b40c356618e77cdde732549b09113ed4a4e9157e7
MD5 742f1f66e6e1a845db1e3e1b2e691ada
BLAKE2b-256 47c47c93aaaba232abc180f1135b035c3da96c64db7ab6a805b1ec22d6841fc0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page