用 Python 查询材料数据 API
读取公开聚合物分页数据,并区分公开读取接口和令牌保护的工作流。
先读取一页
公开实验聚合物接口允许匿名 GET。下例在名称、PSMILES 和引用中搜索 polyethylene,单页最多返回两条。
继续分页
使用 pagination.has_next 判断是否需要请求下一页,保持 search 和 page_size 不变。请求成功但无记录表示没有符合条件的数据。
处理访问和错误
受保护的请求需要 Authorization: Token <TOKEN>。用自己的账号获取令牌,不将其写入日志。向此只读端点发送 POST 会返回 HTTP 405 和 Method not allowed。
示例输入或输出
import requests
response = requests.get(
"https://cleanenergymaterials.cn/polymer/api/public/experiment-polymer-data/",
params={"search": "polyethylene", "page": 1, "page_size": 2},
timeout=20,
)
response.raise_for_status()
result = response.json()
if not result["success"]:
raise RuntimeError(result["error"])
for row in result["data"]:
print(row["Name"], row["PSMILES"], row["Reference"])
print("has_next:", result["pagination"]["has_next"])