kuchuluk
@kuchuluk

Почему python не видит объявленную переменную?

Здравствуйте!
Простой парсер, все модули установил, вот код:
import undetected_chromedriver
import time

try:
    driver = undetected_chromedrive.Chrome()
    driver.get("https://intoli.com/blog/not-possible-to-block-chrome-headless/chrome-headless-test.html")
    time(25)
except Exception as ex:
    print(ex)
finally:
    driver.close()
    driver.quit()

И выдает ошибку:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.

This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:

if __name__ == '__main__':
freeze_support()
...

The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
Traceback (most recent call last):
File "", line 1, in
File "C:\Users\Dell\AppData\Local\Programs\Python\Python310\lib\multiprocessing\spawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python310\lib\multiprocessing\spawn.py", line 125, in _main
prepare(preparation_data)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python310\lib\multiprocessing\spawn.py", line 236, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Users\Dell\AppData\Local\Programs\Python\Python310\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
main_content = runpy.run_path(main_path,
File "C:\Users\Dell\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 289, in run_path
return _run_module_code(code, init_globals, run_name,
File "C:\Users\Dell\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 96, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "C:\Users\Dell\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "E:\OSPanel\domains\mytests.local\parsetests\main.py", line 13, in
driver.close()
NameError: name 'driver' is not defined
Traceback (most recent call last):
File "E:\OSPanel\domains\mytests.local\parsetests\main.py", line 7, in
driver = undetected_chromedriver.Chrome()
File "E:\OSPanel\domains\mytests.local\parsetests\venv\lib\site-packages\undetected_chromedriver\__init__.py", line 388, in __init__
self.browser_pid = start_detached(
File "E:\OSPanel\domains\mytests.local\parsetests\venv\lib\site-packages\undetected_chromedriver\dprocess.py", line 37, in start_detach
ed
pid = reader.recv()
File "C:\Users\Dell\AppData\Local\Programs\Python\Python310\lib\multiprocessing\connection.py", line 255, in recv
buf = self._recv_bytes()
File "C:\Users\Dell\AppData\Local\Programs\Python\Python310\lib\multiprocessing\connection.py", line 310, in _recv_bytes
waitres = _winapi.WaitForMultipleObjects(
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "E:\OSPanel\domains\mytests.local\parsetests\main.py", line 13, in
driver.close()
NameError: name 'driver' is not defined

Почему он не определяет переменную driver
  • Вопрос задан
  • 197 просмотров
Пригласить эксперта
Ответы на вопрос 1
Vindicar
@Vindicar
RTFM!
Посмотри внимательно, у тебя опечатка.
undetected_chromedrive - куда последняя r пропала?

Далее, у тебя кривая логика обработки ошибок. Блок finally подразумевает, что driver был успешно сконструирован - а это не факт. Тебе нужно два блока - один try-except для обработки ошибок программы, и вложенный try-finally - для работы с успешно сконструированным driver.

Далее, что за time(25)? Это бессмыслица, так как time - это модуль.

И вообще, судя по тексту ошибки, такое чувство, что ты пытаешься сразу собрать скрипт в EXE-файл. Отладь сначала скрипт как есть.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы