知识网2022年03月05日 21:14原创
在定制开发爬虫之时,我们会常常需要模拟浏览器进行页面的访问和操作,PhantomJS就是这样一个解决方案,帮助我们模拟用户的操作和访问。在使用该框架之时,发现了如下错误信息:
平台附属信息:Python3.5,windows7.
driver = webdriver.PhantomJS(executable_path=r'D:/Program Files/phantomjs-2.1.1-windows/bin/phantomjs') Traceback (most recent call last): File &<ipython-input-10-5a05d2dc2ee7>&, line 1, in <module> driver = webdriver.PhantomJS(executable_path=r'D:/Program Files/phantomjs-2.1.1-windows/bin/phantomjs') File &D:Program Filesadaconda3libsite-packagesseleniumwebdriverphantomjswebdriver.py&, line 51, in __init__ log_path=service_log_path) File &D:Program Filesadaconda3libsite-packagesseleniumwebdriverphantomjsservice.py&, line 50, in __init__ service.Service.__init__(self, executable_path, port=port, log_file=open(log_path, 'w')) PermissionError: [Errno 13] Permission denied: 'ghostdriver.log'
经过分析其中的错误信息发现,其没有权限写入ghostdriver.log的日志文件,在windows下还需要什么权限?我一时没有想明白。另外ghostdriver.log并未出现在我们的代码中,其应该是PhantomJS自身携带的日志信息文件。
首先我猜测是由于日志文件的权限问题造成的,但是在windows下其实没有什么权限控制的,当下用户都是管理员,权限应该不是的。那就换一个思路分析问题,一般情况下日志文件都是允许自定义或者指定的,这里的这个ghostdriver.log应该是缺省的日志文件,能否重新指定日志文件,从而规避此问题的呢?
于是,我们将代码的配置信息修改如下:
driver = webdriver.PhantomJS(executable_path=r'D:/Program Files/phantomjs-2.1.1-windows/bin/phantomjs', service_log_path=r&E:/watchlog.log&)
这里最主要的修改是设置了service_log_path的日志路径,指定为本地的某个目录文件。重新执行程序之后,问题消失(前面的executable_path如何你phantomjs已经加入了环境变量中,此处可以不写)。
3.总结
基于常理来推测问题,绝大多数情况下都是使用上的不正确造成的问题,故应该比较容易得到解决。
很赞哦!(6)
©芃睿知识网 版权所有 2012-2023 shsongjiang.com
版权申明:本站部分文章来自互联网,如有侵权,请联系邮箱xiajingzpy@163.com,我们会及时处理和回复!