我正在尝试将 selenium/phantomjs 与 scrapy 一起使用,但我遇到了很多错误。例如,采用以下代码片段:
def parse(self, resposne):
while True:
try:
driver = webdriver.PhantomJS()
# do some stuff
driver.quit()
break
except (WebDriverException, TimeoutException):
try:
driver.quit()
except UnboundLocalError:
print "Driver failed to instantiate"
time.sleep(3)
continue
很多时候驱动程序似乎无法实例化(因此 driver
未绑定(bind),因此出现异常),我得到了简介(以及我输入的打印消息)
Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of <selenium.webdriver.phantomjs.service.Service object at 0x7fbb28dc17d0>> ignored
谷歌搜索,似乎每个人都建议更新我拥有的 phantomjs(1.9.8
从源代码构建)。谁知道还有什么可能导致此问题和合适的诊断?
最佳答案
此行为的原因是 PhantomJS 驱动程序的 Service
class已实现。
定义了一个调用self.stop()
方法的__del__
方法:
def __del__(self):
# subprocess.Popen doesn't send signal on __del__;
# we have to try to stop the launched process.
self.stop()
并且,self.stop()
假设服务实例仍然存在并试图访问它的属性:
def stop(self):
"""
Cleans up the process
"""
if self._log:
self._log.close()
self._log = None
#If its dead dont worry
if self.process is None:
return
...
这个线程中完美地描述了同样的问题:
你应该做的是静静地忽略在退出驱动程序实例时发生的AttributeError
:
try:
driver.quit()
except AttributeError:
pass
问题是由这个 revision 引入的.这意味着降级到 2.40.0
也会有所帮助。
关于python - Scrapy with selenium, webdriver 无法实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27674088/