forked from antoine-palazz/Projet-Python-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchdata.py
42 lines (26 loc) · 930 Bytes
/
fetchdata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import requests
import pandas as pd
from urllib.parse import quote
batch_size = 10000
after = 1
api_root = "https://koumoul.com/data-fair/api/v1/datasets/dpe-v2-logements-existants/lines"
def get_dpe_batch(url):
response = requests.get(url)
return response
def get_dpe(var, size=float('inf')):
variables = quote(",".join(var))
url_api = f"{api_root}?after={after}&size={batch_size}&select={variables}"
results = []
total_fetched = 0
while url_api and total_fetched < size:
response = get_dpe_batch(url_api)
if response.status_code == 200:
data = response.json()
results.extend(data['results'])
url_api = data.get('next')
total_fetched = len(results)
else:
print("Failed to fetch data")
break
print(f"Fetched {total_fetched} observations")
return pd.json_normalize(results)