在Python/Selenium中使用PhantomJs

在Python/Selenium中使用PhantomJs

问题描述:

我正在尝试PhantomJS,以便使用Selenium和Python运行我的webscrapes,而不需要在scrape循环进入下一页时打开新窗口。在Python/Selenium中使用PhantomJs

我咨询这个帖子最初:Is there a way to use PhantomJS in Python?

但是我不得不修改我的道路,以获得我下面的代码node.exe:

for link in soup1.findAll('a', {'property_title'}): 
     #print 'https://www.tripadvisor.com/Restaurant_Review-g294217-' + link.get('href') 
     restaurant_url = 'https://www.tripadvisor.com/Restaurant_Review-g188590-' + link.get('href') 
     driver = webdriver.PhantomJS(r"C:\Program Files (x86)\nodejs\node.exe") 
     driver.get(restaurant_url) 
     neighborhood = driver.find_element_by_xpath(r'//*[@id="BODYCON"]/div[2]/div/div[2]/div[2]/div[1]/div[1]/div[2]/div[2]/div[2]/ul/li[3]') 
     restneighborhood = neighborhood.text 
     print restneighborhood 

我得到这个错误:

Traceback (most recent call last): 
    File "C:/Users/dtrinh/PycharmProjects/TripAdvisorData/LinkPull-HK.py", line 23, in <module> 
    driver = webdriver.PhantomJS(r"C:\Program Files (x86)\nodejs\node.exe") 
    File "C:\Python27\lib\site-packages\selenium-3.0.1-py2.7.egg\selenium\webdriver\phantomjs\webdriver.py", line 52, in __init__ 
self.service.start() 
    File "C:\Python27\lib\site-packages\selenium-3.0.1-py2.7.egg\selenium\webdriver\common\service.py", line 86, in start 
self.assert_process_still_running() 
    File "C:\Python27\lib\site-packages\selenium-3.0.1-py2.7.egg\selenium\webdriver\common\service.py", line 99, in assert_process_still_running 
% (self.path, return_code) 
selenium.common.exceptions.WebDriverException: Message: Service C:\Program Files (x86)\nodejs\node.exe unexpectedly exited. Status code was: 9 

我不知道我在做什么,因为我通常使用ChromeDriver,但如果任何人都可以帮助我,那将是非常棒的。

我的PhantomJS文件不在我的脚本文件夹中。把它放在Python27下的那个文件夹中。