爬虫之PermissionError: [Errno 13] Permission denied: 'ghostdriver.log'问题解决

来源:互联网 发布:英雄联盟mac有国服? 编辑:程序博客网 时间:2024/06/05 03:17

1. 问题提出

   在定制开发爬虫之时,我们会常常需要模拟浏览器进行页面的访问和操作, PhantomJS就是这样一个解决方案,帮助我们模拟用户的操作和访问。在使用该框架之时,发现了如下错误信息:

     平台附属信息:  Python 3.5,   windows 7.

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 Files\adaconda3\lib\site-packages\selenium\webdriver\phantomjs\webdriver.py", line 51, in __init__    log_path=service_log_path)  File "D:\Program Files\adaconda3\lib\site-packages\selenium\webdriver\phantomjs\service.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自身携带的日志信息文件。

2. 问题的解决

   首先我猜测是由于日志文件的权限问题造成的,但是在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的日志路径,指定为本地的某个目录文件。重新执行程序之后,问题消失。

3.  总结

     基于常理来推测问题,绝大多数情况下都是使用上的不正确造成的问题,故应该比较容易得到解决。

0 0
原创粉丝点击