Home > other >  Access data through an API with Python
Access data through an API with Python

Time:07-20

I am a complete beginner in the use of API in python. I would like to access this data about Covid 19 vaccination : https://datavaccin-covid.ameli.fr/explore/dataset/donnees-de-vaccination-par-commune/api/

I tried some stuff and it's never working can someone please help me.

This the code I wrote :

import requests

import json

import pandas as pd

query = """{
"nhits":183120,
"dataset":"donnees-de-vaccination-par-commune",
"rows":10,
"start":0,
"facet":[
"semaine_injection",
"commune_residence",
"libelle_commune",
"classe_age",
"libelle_classe_age"
 ],
 "format":"json",
 "timezone":"UTC"

 }""" 


url = 'https://datavaccin-    covid.ameli.fr/api/records/1.0/search/?dataset=donnees-de-vaccination-par-commune&q=&facet=semaine_injection&facet=commune_residence&facet=libelle_commune&facet=classe_age&facet=libelle_classe_age&refine.commune_residence=13001'


r = requests.post(url, json= query)
print(r.status_code)
print(r.text)

and I get an error 400

The url I used is the link at the end of the website, I am not sure it is the good one to use : url used

Thank you

CodePudding user response:

It looks like you're using the "POST" method when you need the "GET" method. The POST method lets you send information to their server. The GET method retrieves only.

r = requests.get(url, json=query)

Also, your query doesn't need all the """ outside the curly braces.

CodePudding user response:

Thanks a lot @James for your help. This works

url = 'https://datavaccin-   covid.ameli.fr/api/records/1.0/search/'

query = {"dataset":"donnees-de-vaccination-par-commune",
"rows":100,
"start":0,
"facet":[
"semaine_injection",
"commune_residence",
"libelle_commune",
"classe_age",
"libelle_classe_age"
],
"format":"json",
"timezone":"UTC"}

r = requests.get(url, params= query)
print(r.status_code)
print(r.text)

I had to use params, change the url and get rid of the triple quotes to make it work

  • Related