A simple package to crawl JSON APIs and save response to local files.
Project description
simplejsonspider
simplejsonspider 是一个超简单的 Python 工具包,用于请求指定的 JSON API 并将返回内容自动保存为本地 JSON 文件。你只需要指定接口地址、文件名模板和存储路径,即可“一键抓取、自动存储”。注意:该工具包仅支持 JSON 格式的 API 响应。用GPT 4.1 只花了15分钟写的。
特点
- ⚡️ 支持自定义 HTTP headers 和 cookies
- 🗂 文件名模板自由拼接,支持如
{id}_{title}.json - 🧰 用法极简,适合数据抓取、接口快照、API调试等场景
安装
pip install simplejsonspider
快速上手
from simplejsonspider import SimpleJSONSpider
id = 1
title = 'delectus aut autem'
# 创建一个简单的 JSON API 抓取器
spider = SimpleJSONSpider(
api_url='https://jsonplaceholder.typicode.com/todos/1', # API接口
filename_template='{id}_{title}.json', # 文件名模板(对应API返回的字段)
storage_dir='./data' # 存储目录
)
spider.run()
执行后,将自动在 ./data 目录下生成如 1_delectus aut autem.json 的文件,内容即API返回的JSON。
进阶用法:自定义headers与cookies
有些网站需要带自定义User-Agent、Referer或Cookie,直接传递即可:
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
"Referer": "https://www.bilibili.com/"
}
cookies = {
"SESSDATA": "your_cookie"
}
spider = SimpleJSONSpider(
api_url='https://jsonplaceholder.typicode.com/todos/1', # 示例API接口
filename_template='{code}.json', # 根据API返回的字段
storage_dir='./bili_data',
headers=headers,
cookies=cookies
)
spider.run()
文件名模板说明
- 支持 Python 的字符串格式化:
{id}_{title}.json - 注意:模板里的字段必须是API响应JSON顶层的键。
常见问题
-
遇到 412、403 等错误? 请添加正确的 User-Agent、Referer 或 Cookie(详见进阶用法)。
-
保存多个API数据? 可在循环中多次创建 SimpleJSONSpider 实例,或自己扩展批量功能。
License
MIT License
关于作者
[Zeturn] [GitHub主页]
---
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simplejsonspider-0.2.2.tar.gz.
File metadata
- Download URL: simplejsonspider-0.2.2.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
35838fde33bc3dd6690296bfd682b1e11af72f838d0f1ce5d5552f4f21acc5d4
|
|
| MD5 |
3436a1f96e363b3d27a166dff10417fd
|
|
| BLAKE2b-256 |
054d9594abb9990073e67fe9a48168a88e7ca3eebf0d35797b820108f695604c
|
File details
Details for the file simplejsonspider-0.2.2-py3-none-any.whl.
File metadata
- Download URL: simplejsonspider-0.2.2-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e0d1d9482057bead1d3ea0db344986298e846b99813bcd1f1b01526a4a198ad
|
|
| MD5 |
ebf6eb38c9c1d7d00099f506df85cb7b
|
|
| BLAKE2b-256 |
df9159b000761683de124811ba25f4cb31008941402376bdf3681a4574909f91
|