我想使用 python 从 https://next-gen.materialsproject.org 下载 cif 文件。我使用了以下代码
from pymatgen.ext.matproj import MPRester
# Initialize MPRester with your Materials Project API key
mpr = MPRester("*********")
# Specify the Materials Project ID (MPID) of the material you want to download the CIF for
mpid = "mp-165" # Example: Si crystal
# Download the structure corresponding to the MPID
structure = mpr.get_structure_by_material_id(mpid)
# Write the structure to a CIF file
cif_filename = f"{mpid}.cif"
structure.to(fmt="cif", filename=cif_filename)
print(f"CIF file downloaded and saved as {cif_filename}")
当我运行此代码时,它显示“Response {”error”:“您正在使用已弃用的 API 端点。请阅读我们的文档 (https://docs.materialsproject.org) 并升级到最新版本的 mp-api 客户端 (https://pypi.org/project/mp-api/)。", “版本”:“已阻止”} ”.
似乎材料项目网站更新了他们的API。我也使用他们网站上给出的代码尝试了新的 API (https://docs.materialsproject.org/downloading-data/using-the-api/examples):
from mp_api.client import MPRester
with MPRester("your_api_key_here") as mpr:
docs = mpr.materials.summary.search(material_ids=["mp-149"], fields=["structure"])
structure = docs[0].structure
# -- Shortcut for a single Materials Project ID:
structure = mpr.get_structure_by_material_id("mp-149")
但是我仍然遇到同样的错误。我正在使用 python 3.8.10。
您可以看到相同的脚本在 Google Colab 上运行良好。
所以这是一个环境问题。
只需创建一个全新的虚拟环境,激活它并安装 mp-api,它就可以工作。
如果您使用的是IDE,请记得在激活虚拟环境后选择相应的Python解释器。