11# !/usr/bin/env python
22from selenium import webdriver
3+ from webdriver_manager.chrome import ChromeDriverManager
34import json
45import requests
56
89# URL = "https://www.geeksforgeeks.org/what-can-i-do-with-python/"
910
1011
11- def download_article(URL ):
12+ def get_driver( ):
1213 # chrome options settings
1314 chrome_options = webdriver.ChromeOptions()
1415 settings = {
@@ -23,16 +24,16 @@ def download_article(URL):
2324 }
2425 chrome_options.add_experimental_option("prefs", prefs)
2526 chrome_options.add_argument("--kiosk-printing")
26- chrome_options.binary_location = (
27- r"C:\Program Files (x86)/"
28- + r"BraveSoftware\Brave-Browser\Application\brave.exe"
29- )
30- CHROMEDRIVER_PATH = r"chromedriver.exe"
3127
3228 # launch browser with predefined settings
3329 browser = webdriver.Chrome(
34- executable_path=CHROMEDRIVER_PATH , options=chrome_options
30+ executable_path=ChromeDriverManager().install() , options=chrome_options
3531 )
32+ return browser
33+
34+
35+ def download_article(URL):
36+ browser = get_driver()
3637 browser.get(URL)
3738
3839 # launch print and save as pdf
@@ -42,8 +43,12 @@ def download_article(URL):
4243
4344if __name__ == "__main__":
4445 URL = input("provide article URL: ")
46+ # check if the url is valid/reachable
4547 if requests.get(URL).status_code == 200:
46- download_article(URL)
47- print("Your article is successfully downloaded")
48+ try:
49+ download_article(URL)
50+ print("Your article is successfully downloaded")
51+ except Exception as e:
52+ print(e)
4853 else:
4954 print("Enter a valid working URL")
0 commit comments