Help to check the python code

I use the python code from official tutorial, but mistake exist:


I find 58 structures in COD
but it only saves 10 structures:
image

The code is as following

import requests

database_url = ‘https://www.crystallography.net/cod/optimade
query = ‘elements HAS ALL “C”, “H”, “N”, “Mn”,“Br” AND nelements=5’
params = {
“filter”: query,
“page_limit”: 3
}

query_url = f"{database_url}/v1/structures"

response = requests.get(query_url, params=params)

json_response = response.json()

print(json_response.keys())
structures = json_response[“data”]
meta = json_response[“meta”]

print(f"Query {query_url} returned {meta[‘data_returned’]} structures")

offset = 0
page_limit = 10
while True:
params = {
“filter”: query,
“page_limit”: page_limit,
“page_offset”: offset
}

response = requests.get(query_url, params=params).json()

file = open('data1.txt', 'w')
for result in response["data"]:
    print(result["attributes"]["chemical_formula_reduced"], result["id"], file=file)

offset += page_limit
if response["meta"]["data_returned"] < offset:
    break

if offset > 100:
    break

It’s hard to tell from your post but it looks like you have edited the code from the tutorials (as you are meant to!) – you are only making 1 request and are only downloading the first page of results. If you look at the example code snippets, they use a for loop to keep requesting pages until there are no more results. You are being told there are 58 results for the filter overall as data_returned is the total count, not the count for the given page.

If you are not familiar with Python you may be happier using one of the existing clients, such as the one in optimade-python-tools Using the OPTIMADE client - OPTIMADE Python tools, the one in pymatgen pymatgen.ext namespace — pymatgen 2023.10.4 documentation or the online clients from Materials Cloud https://optimadeclient.materialscloud.io/ and https://optimade.science.