3
3
import argparse
4
4
from time import sleep
5
5
from pathlib import Path
6
+ from os import access , X_OK
6
7
from typing import Optional
7
8
8
9
DEFAULT_PATH = Path .home () / ".chromedriver"
@@ -115,6 +116,19 @@ def download_chromedriver(filename: str, download_url: str, destdir: Optional[Pa
115
116
return executable_path
116
117
117
118
119
+ def try_make_executable (executable_path : Path ):
120
+ """
121
+ Try to make the chromedriver executable. Return True if successful, False otherwise.
122
+ """
123
+ try :
124
+ print (f"Making { executable_path } executable..." )
125
+ executable_path .chmod (executable_path .stat ().st_mode | X_OK )
126
+ except Exception as e :
127
+ print (f"Failed to make { executable_path } executable. Error: { e } " )
128
+
129
+ return access (executable_path , X_OK )
130
+
131
+
118
132
def get_chromedriver () -> Optional [Path ]:
119
133
parser = argparse .ArgumentParser ()
120
134
parser .add_argument ("-v" , "--version" , type = int , help = "Chromedriver version number" )
@@ -136,6 +150,19 @@ def get_chromedriver() -> Optional[Path]:
136
150
return None
137
151
138
152
print ("Success. Chromedriver executable downloaded and saved to:\n " , executable_path )
153
+
154
+ if not try_make_executable (executable_path ):
155
+ print ("Failed to make chromedriver executable. You may need to do this manually." )
156
+ print ("To make the chromedriver executable, run the following command:" )
157
+ print (f"chmod +x { executable_path } " )
158
+ else :
159
+ print ("Chromedriver is now executable." )
160
+
161
+ print ("\n You can now use the chromedriver with SouperScraper. For example:" )
162
+ print ("from souperscraper import SouperScraper" )
163
+ print (f'scraper = SouperScraper("{ executable_path !s} ")' )
164
+ print ("scraper.goto('https://example.com')" )
165
+ print ("header = scraper.soup.find('h1').text" )
139
166
return executable_path
140
167
141
168
0 commit comments