python运行爬虫程序报错如下:
原因
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\twisted\internet\defer.py", line 1660, in _inlineCallbacks
result = current_context.run(gen.send, result)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\scrapy\crawler.py", line 85, in crawl
self.spider = self._create_spider(*args, **kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\scrapy\crawler.py", line 108, in _create_spider
return self.spidercls.from_crawler(self, *args, **kwargs)
File "E:\PowerfulCrawl\PowerfulCrawl\spiders\powerful_crawl.py", line 160, in from_crawler
spider = super(PowerfulCrawlSpider, cls).from_crawler(crawler, *args, **kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\scrapy\spiders\__init__.py", line 50, in from_crawler
spider = cls(*args, **kwargs)
File "E:\PowerfulCrawl\PowerfulCrawl\spiders\powerful_crawl.py", line 135, in __init__
self.driver = Chrome(chrome_options=self.option, executable_path=self.settings.get('CHROME_DRIVER_PATH'))
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
File "D:\Program Files (x86)\PyCharm Community Edition 2022.2\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_monkey.py", line 578, in new_CreateProcess
return getattr(_subprocess, original_name)(app_name, patch_arg_str_win(cmd_line), *args)
builtins.OSError: [WinError 193] %1 不是有效的 Win32 应用程序。
原因是
self.driver = Chrome(chrome_options=self.option, executable_path=self.settings.get('CHROME_DRIVER_PATH'))这行代码引起的
根本原因是settings.py中的
CHROME_DRIVER_PATH = r'/PowerfulCrawl/PowerfulCrawl/driver/chromedriver'
配置错误,改成chromedriver真实的地址就可以了。