Created
May 22, 2025 13:20
-
-
Save ThomasG77/68840d19d320a0ed83f56df51ec84b48 to your computer and use it in GitHub Desktop.
OCSGE téléchargement IGN flux Atom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.etree.ElementTree as ET | |
import requests | |
base_url_ocsge = "https://data.geopf.fr/telechargement/resource/OCSGE" | |
limit = 50 | |
first_page = f'{base_url_ocsge}?page=1&limit={limit}' | |
r_first_page = requests.get(first_page) | |
root = ET.fromstring(r_first_page.text) | |
print(root.attrib) | |
pages = int(root.attrib['{https://data.geopf.fr/annexes/ressources/xsd/gpf_dl.xsd}pagecount']) | |
keys_entries = [[j.tag for j in i] for i in root.findall('{http://www.w3.org/2005/Atom}entry')] | |
unique_keys_for_entry = list(set([element for sublist in keys_entries for element in sublist])) | |
all_entries = [] | |
for page in range(1, pages + 1): | |
url = f'{base_url_ocsge}?page={page}&limit={limit}' | |
res = requests.get(url) | |
root = ET.fromstring(res.text) | |
for i in root.findall('{http://www.w3.org/2005/Atom}entry'): | |
entry_dicts = [] | |
for j in unique_keys_for_entry: | |
tag_no_ns = j.split('}')[1] | |
if i.find(j) is not None: | |
if 'label' in i.find(j).attrib: | |
entry_dicts.append([tag_no_ns, i.find(j).attrib['label']]) | |
else: | |
entry_dicts.append([tag_no_ns, i.find(j).text]) | |
else: | |
entry_dicts.append([tag_no_ns, '']) | |
all_entries.append(dict(entry_dicts)) | |
filtered_entries = [] | |
for i in all_entries: | |
if ('2021' in i['id'] or '2022' in i['id'] or '2023' in i['id']) and 'DIFF' not in i['id']: | |
filtered_entries.append(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment