我正在用PhantomJS和Selenium抓取网站。我的问题是,在检查了大约50个URL之后,我有一个错误:
selenium.common.exceptions.WebDriverException:消息:无法连接到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发布于 2014-12-29 15:20:04
不要打开/退出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()https://stackoverflow.com/questions/27691364
复制相似问题