From 5d663bcf604a37f5b6d90cd1a4d81c6a63cf48bc Mon Sep 17 00:00:00 2001 From: Bastien Le Querrec <blq@laquadrature.net> Date: Sun, 17 Mar 2024 22:35:13 +0100 Subject: [PATCH] =?UTF-8?q?pref13:=20n'analyse=20que=20les=205=20derni?= =?UTF-8?q?=C3=A8res=20ann=C3=A9es=20disponibles?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RAAspotter_pref13.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/RAAspotter_pref13.py b/RAAspotter_pref13.py index 8d1c269..d47f2a0 100644 --- a/RAAspotter_pref13.py +++ b/RAAspotter_pref13.py @@ -9,7 +9,12 @@ class RAAspotter_pref13(RAAspotter): # Config __HOST = 'https://www.bouches-du-rhone.gouv.fr' - __RAA_PAGE = f'{__HOST}/Publications/RAA-et-Archives' + __RAA_PAGE = [f'{__HOST}/Publications/RAA-et-Archives/RAA-2024', + f'{__HOST}/Publications/RAA-et-Archives/RAA-2023', + f'{__HOST}/Publications/RAA-et-Archives/Archives-RAA-des-Bouches-du-Rhone/RAA-2022', + f'{__HOST}/Publications/RAA-et-Archives/Archives-RAA-des-Bouches-du-Rhone/RAA-2021', + f'{__HOST}/Publications/RAA-et-Archives/Archives-RAA-des-Bouches-du-Rhone/RAA-2020', + f'{__HOST}/Publications/RAA-et-Archives/Archives-RAA-des-Bouches-du-Rhone/RAA-2019'] __USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36' def __init__(self, data_dir): @@ -20,13 +25,10 @@ class RAAspotter_pref13(RAAspotter): self.print_output('RAAspotter_pref13') self.print_output(f'Termes recherchés: {keywords}') - page_content = self.get_page(self.__RAA_PAGE).content - sub_pages = self.get_sub_pages(page_content, 'div.fr-card__body div.fr-card__content h2.fr-card__title a', self.__HOST) - raa_elements = [] - for page in sub_pages: - for element in self.get_raa_elements(self.get_page(page).content): - raa_elements.append(element) - self.parse_raa(raa_elements, keywords.split(',')) + for raa_page in self.__RAA_PAGE: + page_content = self.get_page(raa_page).content + raa_elements = self.get_raa_elements(page_content) + self.parse_raa(raa_elements, keywords.split(',')) def get_raa_elements(self, page_content): elements = [] @@ -34,15 +36,15 @@ class RAAspotter_pref13(RAAspotter): soup = BeautifulSoup(page_content, 'html.parser') # Pour chaque balise a, on regarde si c'est un PDF, et si oui on le parse - for a in soup.find_all('a', href=True): + for a in soup.find_all('a', href=True, class_='fr-link--download'): if a['href'].endswith('.pdf'): if a['href'].startswith('/'): url = f"{self.__HOST}{a['href']}" else: url = a['href'] - name = unquote(url.split('/')[-1]) - date = "date non renseignée" + name = a.find('span').previous_sibling.replace('Télécharger ', '').strip() + date = a.find('span').get_text().split(' - ')[-1].strip() filename = unquote(url.split('/')[-1]) raa = RAAspotter.RAA(url, date, name, filename) -- GitLab