首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PhantomJS -连接到GhostDriver错误

PhantomJS -连接到GhostDriver错误
EN

Stack Overflow用户
提问于 2014-12-29 15:12:53
回答 1查看 1.6K关注 0票数 4

我正在用PhantomJS和Selenium抓取网站。我的问题是,在检查了大约50个URL之后,我有一个错误:

selenium.common.exceptions.WebDriverException:消息:无法连接到GhostDriver

我不知道如何修复它,我尝试了两个PhantomJS版本(1.9和1.98),但它仍然无法工作。你知不知道?

下面是我正在执行的代码:

代码语言:javascript
复制
def get_car_price(self, car_url): 
    browser = webdriver.PhantomJS('C:\phantomjs.exe') 
    browser.get(car_url) 
    content = browser.page_source 
    browser.quit() 

    website = lh.fromstring(content) 
    for price in website.xpath('//*[@id="js_item_' + str(self.car_id) + '"]/div[1]/div[2]/div[2]/strong[2]'): 
        return price.text
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-29 15:20:04

不要打开/退出PhantomJS浏览器,保持打开并重用它。在脚本启动时全局创建它,并在脚本即将完成时退出。

示例:

代码语言:javascript
复制
class Service(object):
    def __init__(self):
        self.browser = webdriver.PhantomJS('C:\phantomjs.exe') 

    def get_car_price(self, car_url): 
        self.browser.get(car_url) 
        content = self.browser.page_source 

        website = lh.fromstring(content) 
        for price in website.xpath('//*[@id="js_item_' + str(self.car_id) + '"]/div[1]/div[2]/div[2]/strong[2]'): 
            return price.text

    def shutdown(self):
        self.browser.quit() 

service = Service()
try:
    for url in urls:
        print(service.get_car_price(url))
finally:
    service.shutdown()
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27691364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档