我开始学习网页抓取,所以我试图在Python中使用PhantomJS。经过几个小时的ghostdriver.log屏障之后,我现在正面对一个NameError:全局名称'PhantomJS'未定义
我正在使用Windows 10机器,我有phantomjs 2.1.1和metaperl :: selenium 2.40.0(这与硒不同吗?)
这是我的代码:
import selenium as sel #i don't think this was needed, but i was trying anything
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.PhantomJS(executable_path='~/Downloads/phantomjs-2.1.1-windows/phantomjs-2.1.1-windows/bin')
driver.set_window_size(1024, 768)
driver.get('https://www.google.com/#q=data+science')
完整的异常消息:
NameErrorTraceback (most recent call last)
<ipython-input-21-f352fd375d8b> in <module>()
4 from selenium.webdriver.common.keys import Keys
5
----> 6 driver = webdriver.PhantomJS(executable_path='~/Downloads/phantomjs-2.1.1-windows/phantomjs-2.1.1-windows/bin')
7 driver.set_window_size(1024, 768)
8 driver.get('https://www.google.com/#q=data+science')
C:\Users\Elizabeth\Anaconda2\lib\site-packages\selenium\webdriver\phantomjs\webdriver.pyc in __init__(self, executable_path, port, desired_capabilities, service_args, service_log_path)
47 """
48 self.service = Service(executable_path, port=port,
---> 49 service_args=service_args, log_path=service_log_path)
50 self.service.start()
51
C:\Users\Elizabeth\Anaconda2\lib\site-packages\selenium\webdriver\phantomjs\service.py in __init__(self, executable_path, port, service_args, log_path)
37 - log_path: Path for PhantomJS service to log to
38 """
---> 39 PhantomJS(service_log_path=os.path.devnull)
40 self.port = port
41 self.path = executable_path
NameError: global name 'PhantomJS' is not defined
我尝试过使用路径名,在该代码之外设置可执行路径,阅读selenium和phantomjs的文档,并在网上搜索了几个小时,我还可以找出其他任何有关它的内容。< / p>