安装phantomjs 报错

本文介绍如何解决在使用pyspider时遇到的PhantomJS环境问题,包括错误提示'QXcbConnection:Couldnotconnecttodisplay',并提供两种解决方案,涉及环境变量设置和代码修改。参考链接提供了详细步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

phantomjs是作为pyspider的一个外部依赖组件部署的。
apt安装完出现问题:

QXcbConnection: Could not connect to display 
PhantomJS has crashed. Please read the bug reporting guide at
<http://phantomjs.org/bug-reporting.html> and file a bug report.
Aborted

解决方案:

方法一:环境变量中导入以下两个变量
方法二:/usr/bin/phantomjs 加上以下设置代码

export QT_QPA_PLATFORM=offscreen
export QT_QPA_FONTDIR=/usr/share/fonts

参考链接:https://blog.youkuaiyun.com/liwancheng91/article/details/79147271

webdriver.PhantomJS是selenium模块中的一个类,用于创建一个PhantomJS浏览器实例,以便进行自动化测试。\[3\]在使用selenium模块时,如果出现了'selenium.common.exceptions.WebDriverException: Message: 'phantomjs' executable needs to be in PATH.'的错误,可以按照以下步骤解决: 1. 下载PhantomJS程序,并根据版本选择合适的下载文件。 2. 将下载的PhantomJS.exe文件放到Python的根目录,即Python.exe所在的目录。如果放到根目录后仍然出现错误,可以尝试将PhantomJS.exe所在的路径添加到系统的环境变量中。 3. 在程序中导入selenium模块,并使用webdriver.PhantomJS()创建PhantomJS浏览器实例,然后可以进行相应的操作。例如: ``` from selenium import webdriver driver = webdriver.PhantomJS() driver.get("http://www.baidu.com") ``` 这样就可以正常使用PhantomJS进行自动化测试了。\[2\]\[3\] #### 引用[.reference_title] - *1* [爬虫之 ---- 无界面浏览器](https://blog.csdn.net/fenglei0415/article/details/80316241)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [python使用selenium模块的webdriver和PhantomJs报错解决](https://blog.csdn.net/weixin_42054155/article/details/103340990)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值