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. codec.py复制到自己的项目中
  2. uv pip install -r requirements.txt,其中关键的是uv pip install jupyter_kernel_client

使用方法

  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'))
    # 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.0.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.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: jupyter_data_fetch-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 0a876a2dad7ff6878f7f53bb0800854aad5bcb860ca72d4ca76f0f8fe5247e3b
MD5 4aeab933004647b38e70077bd45711a4
BLAKE2b-256 440211c41d5559ad6a5ccdd976f4793ad1f7108c26b516aad6019b4104689e0f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jupyter_data_fetch-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b6bae2f8e9539698119bb65ee9ab6c4fcc3d1c61eb533d8d440b3d109af46267
MD5 cbe3e2233eb15488627f5412a5b31b1b
BLAKE2b-256 25fe81da2aac3d6125acc2d96ce7024d5480d43ac73b552d49dd20e1c6f65077

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