Can anyone help me understand why my code does not work? If you type a search term into the UI at https://gtr.ukri.org/ you get back a list of relevant projects, but using the exact same search term as keyword in my code returns zilch. The reasons for my struggle include: (1) I am new to code and without formal education in logical/technical subjects; (2) I cannot find the correct header format in the documentation; (3) many other difficulties. I would really love to understand why my simple code does not work. If you can make it work I will be amazed and very happy!
Here is my code. I have put “neurotechnology” as my search term because that is the area of interest to me.
import requests
ENDPOINT = f"https://gtr.ukri.org/api/search/project?term=neurotechnology"
HEADERS = {
"Accept": "application/json"
}
response = requests.get(ENDPOINT, headers=HEADERS)
if response.status_code == 200:
data = response.json()
if 'projectsBean' in data and 'projects' in data['projectsBean']:
projects = data['projectsBean']['projects']
for i, project in enumerate(projects):
project_info = {
"title": project.get('title'),
"grant_ref": project.get('grantReference'),
"host_institution": project.get('leadResearchOrganisation', {}).get('name'),
"start_date": project.get('fund', {}).get('start'),
"fund_amount": project.get('fund', {}).get('valuePounds'),
"funder": project.get('fund', {}).get('funder', {}).get('name'),
}
project_dict[f'project_{i + 1}'] = project_info
print(project_dict)
else:
print("No data.")
else:
print(f"Error status code: {response.status_code}")
I have tried looking through the documentation on the GtR API pages and while some things are clear, other things cannot be understood by me. I have also exhausted my personal capacity to extract help from ChatGPT even having fed it all the documentation. I have reached the limit of ideas that I have learned so far in my online course (100 Days of Code, Dr Angelu Yu. I’m on Day 36).
1