From 7c376a65a10de8dd76ba12df6e2f444b5dae7a5c Mon Sep 17 00:00:00 2001 From: Benjamin Loison <12752145+Benjamin-Loison@users.noreply.github.com> Date: Tue, 2 Apr 2024 02:04:28 +0200 Subject: [PATCH] Add the resumable part of the code --- main.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index bfa797e..08cc4f1 100755 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ import requests from lxml import html import string +from pathlib import Path charset = string.ascii_lowercase url = 'https://www.linguee.fr/francais-anglais/search' @@ -14,16 +15,25 @@ MAXIMUM_SUGGESTIONS = 4 entries = set() +REQUESTS_FOLDER_PATH = 'requests' +Path(REQUESTS_FOLDER_PATH).mkdir(exist_ok = True) + def treatSuffixes(prefix): #print(prefix) for char in charset: base = prefix + char print(base) - params['qe'] = base - text = requests.get(url, params = params).text - # Pay attention if change `base` elaboration to not allow unwanted folder file writing. - with open(f'requests/{base}.html', 'w') as requestFile: - requestFile.write(text) + baseFilePath = f'{REQUESTS_FOLDER_PATH}/{base}.html' + try: + with open(baseFilePath) as requestFile: + text = requestFile.read() + print('From file') + except: + params['qe'] = base + text = requests.get(url, params = params).text + # Pay attention if change `base` elaboration to not allow unwanted folder file writing. + with open(baseFilePath, 'w') as requestFile: + requestFile.write(text) tree = html.fromstring(text) rows = tree.xpath('//div[@class="main_row"]') rowsLen = len(rows)