From f9be405e5eb1d1d86a119b91ee270a0302e606bc Mon Sep 17 00:00:00 2001 From: lidor Date: Sun, 7 Jul 2024 23:05:03 +0300 Subject: [PATCH] Add main.py --- main.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..564a1a0 --- /dev/null +++ b/main.py @@ -0,0 +1,43 @@ +import time +import base64 +from selenium import webdriver + + +def download_images(url, destination_folder, chromedriver_path): + chromeOptions = webdriver.ChromeOptions() + prefs = {"download.default_directory": destination_folder} + chromeOptions.add_experimental_option("prefs", prefs) + driver = webdriver.Chrome(executable_path=chromedriver_path, chrome_options=chromeOptions) + driver.get(url) + driver.implicitly_wait(10) + pages = driver.find_elements_by_class_name("page-controls__page-numbers") + page_count = int(pages[0].text.split(' / ')[1]) + default_index = 3 + for index in range(page_count): + try: + canvas = driver.find_element_by_xpath(f'//*[@id="ird3-main"]/div/div/div[{default_index}]/div[1]/div[1]/canvas') + base64_image = base64.b64decode(canvas.screenshot_as_base64) + # save to a file + with open(rf"{destination_folder}\\image_{index}.jpg", 'wb') as f: + f.write(base64_image) + print(f'image_{index}.jpg downloaded!') + next_button = driver.find_element_by_id('ird3-button-next') + next_button.click() + time.sleep(5) + except Exception as ex: + print(ex.msg) + continue + + +if __name__ == '__main__': + # download python 3.9.7 + # pip install selenium + # The image saving path: + destination_folder = r"C:\Users\USER\Downloads\BookCoursePDF" + # You must download a chromedriver and specify a full path to it + chromedriver_path = r"C:\Users\USER\Downloads\Compressed\chromedriver.exe" + # URL of the flipbooks/PDF website + url = "https://issuu.com/Website-address" + print("Start") + download_images(url, destination_folder, chromedriver_path) + print("Done.")