From e709259a8f0a580b99915ded675a1f01d835b244 Mon Sep 17 00:00:00 2001 From: Suwako Moriya Date: Sun, 11 Nov 2018 09:39:34 +0100 Subject: [PATCH] Moved interpreter code into a function and ran flake8 (except for E501) --- fetch.py | 36 ++++++++++++++++++++---------------- fetchbs.py | 6 ++++-- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/fetch.py b/fetch.py index f613312..d849ff0 100755 --- a/fetch.py +++ b/fetch.py @@ -11,34 +11,34 @@ import re # Variables -api = "https://master.apis.dev.openstreetmap.org/" # Testing, it should be replaced by https://api.openstreetmap.org/ when the program is finished +api = "https://master.apis.dev.openstreetmap.org/" # Testing, it should be replaced by https://api.openstreetmap.org/ when the program is finished -def runfetch(url): - session=requests.Session() - req1=session.get(url) # Get the source code - text=req1.text.split('\n') +def runfetch(url='https://www.legifrance.gouv.fr/eli/arrete/2018/10/12/PRMD1824595A/jo/texte'): + session = requests.Session() + req1 = session.get(url) # Get the source code + text = req1.text.split('\n') # Parse the HTML source code - text=['\n'.join(row.split('\n')[:row.split('\n').index(""):]) for row in '\n'.join(text[text.index('
ANNEXES
ANNEXE I

'):text.index('
Annexe
'):]).split('')[2::]] - diclist=[] + text = ['\n'.join(row.split('\n')[:row.split('\n').index(""):]) for row in '\n'.join(text[text.index('
ANNEXES
ANNEXE I

'):text.index('
Annexe
'):]).split('')[2::]] + diclist = [] for row in text: - cols=[text.split('\n')[2][5::] if len(text.split('\n'))==3 else "" for text in row.split("", "\n", diclist[index][element]) # Replace
,
and
by \n + diclist[index][element] = re.sub("", "\n", diclist[index][element]) # Replace
,
and
by \n # Make a the area for area in diclist: @@ -47,7 +47,7 @@ if __name__ == '__main__': # Multiple polygons if "polygones" in area["zone"]: lst = [] - tmp = re.compile(".*zone.*").split(area["zone"]) # List of polygons + tmp = re.compile(".*zone.*").split(area["zone"]) # List of polygons del tmp[0] for polygon in tmp: lst.append(re.findall(".{0,35} ?:? ?.? ?: ?(\d{1,3}° \d{2}' \d{2},?\d{0,2}(?:\"|”) .) ?\/ ?(\d{1,3}° \d{2}' \d{2},?\d{0,2}(?:\"|”) .)", polygon)) @@ -83,8 +83,12 @@ if __name__ == '__main__': # Atolls elif area["zone"].strip() == "atolls et eaux territoriales incluses": area["zone"] = [("cercle", "35KM", "138.9022", "21.82917"), ("cercle", "27KM", "138.7425", "22.23528A")] + return diclist +if __name__ == '__main__': + + diclist = fetch() # Fetch all the data from a french article # Print the dict (keep this code in the end of the file) print("===== Dict =====") @@ -93,4 +97,4 @@ if __name__ == '__main__': print(f"\n-------------{index}----------------\n") for element in area: print(f" {element}: {diclist[index][element]}") - print("\n") \ No newline at end of file + print("\n") diff --git a/fetchbs.py b/fetchbs.py index 6392569..8c4a004 100755 --- a/fetchbs.py +++ b/fetchbs.py @@ -1,10 +1,11 @@ #!/usr/bin/env python3 -#-*- coding: utf-8 -*- +# -*- coding: utf-8 -*- # Imports import requests from bs4 import BeautifulSoup + def runfetch(): site = requests.get("https://www.legifrance.gouv.fr/eli/arrete/2018/10/12/PRMD1824595A/jo/texte").text soup = BeautifulSoup(site, "html.parser") @@ -16,5 +17,6 @@ def runfetch(): tds[-1].append(td.text) print(trs) + if __name__ == "__main__": - runfetch() \ No newline at end of file + runfetch()