gpt4 book ai didi

python - PhantomJS - 连接到 GhostDriver 错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:02:57 25 4
gpt4 key购买 nike

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

selenium.common.exceptions.WebDriverException: Message: Can not connect to GhostDriver

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

这是我正在执行的代码:

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

最佳答案

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

例子:

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()

关于python - PhantomJS - 连接到 GhostDriver 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27691364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com