@@ -101,34 +101,39 @@ def download_image(browser, url, save_directory):
101
101
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
102
102
}
103
103
if is_valid_url (url ):
104
- # 发送 GET 请求获取图片数据
105
- response = requests .get (url , headers = headers )
106
-
107
- # 检查响应状态码是否为成功状态
108
- if response .status_code == requests .codes .ok :
109
- # 提取文件名
110
- file_name = url .split ('/' )[- 1 ].split ("?" )[0 ]
111
-
112
- # 生成唯一的新文件名
113
- new_file_name = file_name + '_' + \
114
- str (uuid .uuid4 ()) + '_' + file_name
115
-
116
- # 构建保存路径
117
- save_path = os .path .join (save_directory , new_file_name )
118
-
119
- # 保存图片到本地
120
- with open (save_path , 'wb' ) as file :
121
- file .write (response .content )
122
-
123
- browser .print_and_log ("图片已成功下载到:" , save_path )
124
- browser .print_and_log ("The image has been successfully downloaded to:" , save_path )
125
- else :
126
- browser .print_and_log ("下载图片失败,请检查此图片链接是否有效:" , url )
127
- browser .print_and_log (
128
- "Failed to download image, please check if this image link is valid:" , url )
104
+ try :
105
+ # 发送 GET 请求获取图片数据
106
+ response = requests .get (url , headers = headers )
107
+
108
+ # 检查响应状态码是否为成功状态
109
+ if response .status_code == requests .codes .ok :
110
+ # 提取文件名
111
+ file_name = url .split ('/' )[- 1 ].split ("?" )[0 ]
112
+
113
+ # 生成唯一的新文件名
114
+ new_file_name = file_name + '_' + \
115
+ str (uuid .uuid4 ()) + '_' + file_name
116
+
117
+ # 构建保存路径
118
+ save_path = os .path .join (save_directory , new_file_name )
119
+
120
+ # 保存图片到本地
121
+ with open (save_path , 'wb' ) as file :
122
+ file .write (response .content )
123
+
124
+ browser .print_and_log ("图片已成功下载到:" , save_path )
125
+ browser .print_and_log (
126
+ "The image has been successfully downloaded to:" , save_path )
127
+ else :
128
+ browser .print_and_log ("下载图片失败,请检查此图片链接是否有效:" , url )
129
+ browser .print_and_log (
130
+ "Failed to download image, please check if this image link is valid:" , url )
131
+ except Exception as e :
132
+ browser .print_and_log ("下载图片失败|Error downloading image: " , e )
129
133
else :
130
134
browser .print_and_log ("下载图片失败,请检查此图片链接是否有效:" , url )
131
- browser .print_and_log ("Failed to download image, please check if this image link is valid:" , url )
135
+ browser .print_and_log (
136
+ "Failed to download image, please check if this image link is valid:" , url )
132
137
133
138
134
139
def get_output_code (output ):
@@ -201,9 +206,9 @@ def write_to_json(file_name, data, types, record, keys):
201
206
except :
202
207
line [i ] = 0.0
203
208
if record [i ]:
204
- to_write .update ({keys [i ]: line [i ]})
209
+ to_write .update ({keys [i ]: line [i ]})
205
210
data_to_write .append (to_write )
206
-
211
+
207
212
try :
208
213
# read data from JSON
209
214
with open (file_name , 'r' , encoding = 'utf-8' ) as f :
@@ -212,11 +217,12 @@ def write_to_json(file_name, data, types, record, keys):
212
217
json_data = []
213
218
214
219
json_data .extend (data_to_write )
215
-
220
+
216
221
# write data to JSON
217
222
with open (file_name , 'w' , encoding = 'utf-8' ) as f :
218
223
json .dump (json_data , f , ensure_ascii = False )
219
224
225
+
220
226
def write_to_excel (file_name , data , types , record ):
221
227
first = False
222
228
if os .path .exists (file_name ):
0 commit comments