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. 下载速度更快。更适合大数据文件。但每个网页都需要调整
  3. 由于权限问题/库无法导入,服务器上文件需要手工删除
  4. 参考examples/download/joinquant.py

使用方法

  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, 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.1

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.1
File Size Uploaded
jupyter_data_fetch-0.2.1.tar.gz 11.3 kB Details

Built distribution (wheel)

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

Total release size: 25.7 kB

Release files / jupyter_data_fetch-0.2.1.tar.gz

Download URL jupyter_data_fetch-0.2.1.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5a6caff62ffab7bbb9cc1b793d37abff242370d8f83835f9710a26eca74bff62
BLAKE2b-256 checksum
How to use checksums
b11ef6053f79a1cb72b89a9d30db0a68bcc7938d5131455431c1bd0ac004b1c4
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.1-py3-none-any.whl

Download URL jupyter_data_fetch-0.2.1-py3-none-any.whl
Size 14.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7422f70190412950f87fe9cc75b3e33ffec7153f6e7a6ef711a87387c4476df3
BLAKE2b-256 checksum
How to use checksums
67c535cdea8986bbf477c3880c00fde264b6f3ce8549874101ba3e50697c4f0c
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

0.2.3

2 release files

0.2.2

2 release files

This release

0.2.1 This release

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