Skip to main content

jupyter-data-fetch

从JupyterLab、Jupyter Notebook、VSCode网页版/code-server中抓取数据的示例

优点

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

安装

  1. uv pip install jupyter-data-fetch -U # Jupyter消息协议版
  2. uv pip install jupyter-data-fetch[playwright] -U # playwright网页自动化版

Jupyter消息协议版(推荐)

  1. 根据Jupyter消息协议,模拟浏览器直接连接服务器进行代码的执行和获取,效率高
  2. 支持JupyterLab、Jupyter Notebook
  3. 参考examples/message

playwright网页自动化版

  1. 网页自动化控制,通用性更高,支持JupyterLab、Jupyter Notebook
  2. 支持VSCode网页版/code-server
  3. 暂时不支持的网站也可以定制开发
  4. 效率较低,因为多了网页渲染
  5. 参考examples/automation

HTTP下载版

  1. 数据获取阶段,不通过网页展示提取数据,而是得到下载地址后HTTP下载
  2. 直接是二进制,不用base64/base85编码,文件更小,下载速度更快。更适合大数据文件。但每个网站都需要针对性调整
  3. 部分平台由于权限问题,服务器上文件可能需要手工删除
  4. 部分平台HTTP下载可能有流量限制
  5. 参考examples/download

使用方法

  1. examples下提供了示例
  2. 以joinquant为例,打开浏览器,登录研究环境,按F12打开开发者工具
  3. 搜索kernels,复制Cookie devtool.png
  4. 替换示例中COOKIE即可 ide.png
  5. 会自动从COOKIE提取用户ID,并更新SERVER_URL

最简示例

from jupyter_kernel_client import KernelClient

from jupyter_data_fetch.codec import TextCodec
from jupyter_data_fetch import extract_from_reply

# ... 省去部分代码。更多参考examples/message/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(TextCodec.generate_code(code, var_name='df'), store_history=False)
    print(reply)
    obj = TextCodec.decode(extract_from_reply(reply))
    print(obj)

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

参考examples/experimental/cookie_playwright.py

核心代码

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

注意

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

Metadata

Release files for jupyter-data-fetch 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jupyter-data-fetch 0.2.3
File Size Uploaded
jupyter_data_fetch-0.2.3.tar.gz 12.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jupyter-data-fetch 0.2.3
File Interpreter ABI Platform
jupyter_data_fetch-0.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 28.8 kB

Release files / jupyter_data_fetch-0.2.3.tar.gz

Download URL jupyter_data_fetch-0.2.3.tar.gz
Size 12.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f8885e423c1b7146a45cb522995fbe021e4dc12828678e5fde35000285ccc731
BLAKE2b-256 checksum
How to use checksums
9aff98cd1e1a555cc7d4ef5c3dfdd3c6581d4e8ad73ff3894332047ec409ccf4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / jupyter_data_fetch-0.2.3-py3-none-any.whl

Download URL jupyter_data_fetch-0.2.3-py3-none-any.whl
Size 16.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a2f3920c76fd7fc0609f0c5b058e3563c0f721afd5d852eb4a8fb8801148f80f
BLAKE2b-256 checksum
How to use checksums
c9ce66eafa07909496807ff77dbd73fee3f7650a0d25743a862e1b30cff30d3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

0.3.0

2 release files

0.2.4

2 release files

This release

0.2.3 This release

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page