Python Selenium和PhantomJS错误:";无法连接到GhostDriver“;

Python Selenium和PhantomJS错误:";无法连接到GhostDriver“;,python,selenium,phantomjs,Python,Selenium,Phantomjs,我正在运行Selenium和PhantomJS将搜索词输入网站,并检索每个搜索词的点击数。我必须完成130000多次,因此代码已经运行了一天,直到程序突然出现以下错误: Traceback (most recent call last): File "CBBPlyNwsScrape.py", line 82, in <module> browser = webdriver.PhantomJS() File "/Library/Python/2.7/site-pack

我正在运行Selenium和PhantomJS将搜索词输入网站,并检索每个搜索词的点击数。我必须完成130000多次,因此代码已经运行了一天,直到程序突然出现以下错误:

Traceback (most recent call last):
  File "CBBPlyNwsScrape.py", line 82, in <module>
     browser = webdriver.PhantomJS()
  File "/Library/Python/2.7/site-packages/selenium/webdriver/phantomjs/webdriver.py", line 50, in __init__
     self.service.start()
  File "/Library/Python/2.7/site-packages/selenium/webdriver/phantomjs/service.py", line 69, in start
     raise WebDriverException("Can not connect to GhostDriver")
selenium.common.exceptions.WebDriverException: Message: 'Can not connect to GhostDriver' 

谢谢

我也有同样的问题。我不知道为什么程序在调用phantomJS webdriver时遇到问题,但答案是编写一个简单的异常WebDriverException。下面的代码为我实现了这个技巧

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException, WebDriverException
import unittest, time, re, urllib2

f = open("mother.txt","r") #opens file with name of "test.txt"
l = "1"
m = "2"
n = "3"
aTuple = ( l, m, n ) # create tuple
e = int(0)


for line in f:          
    e += 1
    try:
        h = str(e)
        j = line
        g = open("yes4/" + h + ".txt","w") #opens file with name of "test.txt"
        for item in aTuple:
            driver = webdriver.PhantomJS('phantomjs')
            base_url = j + item
            verificationErrors = []
            accept_next_alert = True
            driver.get(base_url)
            elem=driver.find_element_by_id("yelp_main_body")
            source_code=elem.get_attribute("outerHTML").encode('utf-8').strip()
            g.write(source_code)
            driver.quit()
    except WebDriverException:
        print "e"       
        h = str(e)
        j = line
        g = open("yes4/" + h + ".txt","w") #opens file with name of "test.txt"
        for item in aTuple:
            driver = webdriver.PhantomJS('phantomjs')
            base_url = j + item
            verificationErrors = []
            accept_next_alert = True
            driver.get(base_url)
            elem=driver.find_element_by_id("yelp_main_body")
            source_code=elem.get_attribute("outerHTML").encode('utf-8').strip()
            g.write(source_code)
            driver.quit()
    else:
        print h

安装最新的幻像JS修复了这个错误,这是默认的Ubuntu 12.04 PHOANTMOJS DESTRO

发生的,您应该考虑在函数中封装普通代码,而不是复制它。在Ubuntu 12.04中,我更新了幻像到版本1.92并修复了问题。
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException, WebDriverException
import unittest, time, re, urllib2

f = open("mother.txt","r") #opens file with name of "test.txt"
l = "1"
m = "2"
n = "3"
aTuple = ( l, m, n ) # create tuple
e = int(0)


for line in f:          
    e += 1
    try:
        h = str(e)
        j = line
        g = open("yes4/" + h + ".txt","w") #opens file with name of "test.txt"
        for item in aTuple:
            driver = webdriver.PhantomJS('phantomjs')
            base_url = j + item
            verificationErrors = []
            accept_next_alert = True
            driver.get(base_url)
            elem=driver.find_element_by_id("yelp_main_body")
            source_code=elem.get_attribute("outerHTML").encode('utf-8').strip()
            g.write(source_code)
            driver.quit()
    except WebDriverException:
        print "e"       
        h = str(e)
        j = line
        g = open("yes4/" + h + ".txt","w") #opens file with name of "test.txt"
        for item in aTuple:
            driver = webdriver.PhantomJS('phantomjs')
            base_url = j + item
            verificationErrors = []
            accept_next_alert = True
            driver.get(base_url)
            elem=driver.find_element_by_id("yelp_main_body")
            source_code=elem.get_attribute("outerHTML").encode('utf-8').strip()
            g.write(source_code)
            driver.quit()
    else:
        print h