2018-11-05 22:50:53 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Created on Mon Nov 5 20:55:36 2018
|
|
|
|
|
|
|
|
@author: suwako
|
|
|
|
"""
|
2018-11-06 22:13:18 +01:00
|
|
|
|
2018-11-05 22:50:53 +01:00
|
|
|
import requests
|
2018-11-07 23:32:46 +01:00
|
|
|
import re
|
2018-11-06 22:13:18 +01:00
|
|
|
|
|
|
|
|
2018-11-05 22:50:53 +01:00
|
|
|
def runfetch(url):
|
|
|
|
session=requests.Session()
|
2018-11-07 23:32:46 +01:00
|
|
|
req1=session.get(url) # Get the source code
|
2018-11-05 22:50:53 +01:00
|
|
|
text=req1.text.split('\n')
|
2018-11-07 23:32:46 +01:00
|
|
|
# Parse the HTML source code
|
2018-11-06 20:55:47 +01:00
|
|
|
text=['\n'.join(row.split('\n')[:row.split('\n').index("</tr>"):]) for row in '\n'.join(text[text.index('<br/>ANNEXES<br/>ANNEXE I</p>'):text.index('<div style="margin-top: 30px; margin-bottom:20px;" id="JORFSCTA000037493059" class="titreSection">Annexe </div>'):]).split('<tr>')[2::]]
|
|
|
|
diclist=[]
|
2018-11-05 22:50:53 +01:00
|
|
|
for row in text:
|
2018-11-06 20:55:47 +01:00
|
|
|
cols=[text.split('\n')[2][5::] if len(text.split('\n'))==3 else "" for text in row.split("</td")[::]]
|
|
|
|
diclist.append({"id":cols[0],"commune":cols[1],"site":cols[2],"departement":cols[3],"zone":cols[4],"ministere":cols[5],"aerozone":cols[6]})
|
2018-11-06 22:13:18 +01:00
|
|
|
#dicformat:{id,commune,site,departement,zone,ministere,aerozone}
|
2018-11-06 20:55:47 +01:00
|
|
|
return diclist
|
2018-11-06 22:13:18 +01:00
|
|
|
|
|
|
|
|
2018-11-05 22:50:53 +01:00
|
|
|
if __name__ == '__main__':
|
2018-11-07 23:32:46 +01:00
|
|
|
diclist=runfetch('https://www.legifrance.gouv.fr/eli/arrete/2018/10/12/PRMD1824595A/jo/texte') # Fetch all the data from a french article
|
2018-11-06 20:55:47 +01:00
|
|
|
url='https://www.legifrance.gouv.fr/eli/arrete/2018/10/12/PRMD1824595A/jo/texte'
|
2018-11-07 23:32:46 +01:00
|
|
|
for index, zone in enumerate(diclist):
|
|
|
|
for element in zone:
|
|
|
|
diclist[index][element] = re.sub("<br.{0,2}>", "\n", diclist[index][element]) # Replace <br>, <br /> and <br/> by \n
|
2018-11-08 00:06:37 +01:00
|
|
|
# Make a the area
|
|
|
|
for area in diclist:
|
|
|
|
# Polygons
|
2018-11-08 21:22:44 +01:00
|
|
|
if "polygone" in area["zone"] or "Polygone" in area["zone"]:
|
|
|
|
lst = re.findall(".{0,35} ?:? ?.? ?: ?(\d{1,3}° \d{2}' \d{2},?\d{0,2}(?:\"|”) .) ?\/ ?(\d{1,3}° \d{2}' \d{2},?\d{0,2}(?:\"|”) .)", area["zone"])
|
|
|
|
area["zone"] = lst
|
|
|
|
del lst
|
|
|
|
elif "cercle" in area["zone"] or "Cercle" in area["zone"]:
|
|
|
|
lst = [()]
|
|
|
|
lst[0] = re.search(r"(.{6}) .{0,5}?(\d{1,3},?\d{0,2} ?.{1,2}) .{1,35} ?(\d{3}° \d{1,2}' \d{1,2}” .) ?\/ ?(\d{1,3}° \d{1,2}' \d{1,2}” .)", area["zone"], re.S).groups()
|
2018-11-08 00:06:37 +01:00
|
|
|
area["zone"] = lst
|
|
|
|
del lst
|
|
|
|
|
|
|
|
# Print the dict (keep this code in the end of the file)
|
2018-11-07 23:10:52 +01:00
|
|
|
print("===== Dict =====")
|
2018-11-08 00:06:37 +01:00
|
|
|
for index, area in enumerate(diclist):
|
2018-11-08 21:22:44 +01:00
|
|
|
if index == 129:
|
|
|
|
print(f"\n-------------{index}----------------\n")
|
|
|
|
for element in area:
|
|
|
|
print(f" {element}: {diclist[index][element]}")
|
|
|
|
print("\n")
|