web python 取代js_Selenium+PhantomJS替代方案

问题描述:

python3在使用selenium+PhantomJS动态抓取网页时,出现如下报错信息:

UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead

warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless

翻译过来就是:selenium已经放弃PhantomJS了,建议使用火狐或者谷歌无界面浏览器。

so,PhantomJS要退出历史舞台了。

解决方案:

selenium版本降级(不推荐)

通过pip show selenium显示,默认安装版本为3.8.1。

将其卸载pip uninstall selenium,重新安装并指定版本号pip install selenium==2.48.0。

再次运行,发现没有报错。

使用无界面浏览器

Selenium+Headless Firefox

Selenium+Headless Firefox和Selenium+Firefox,区别就是实例option的时候设置-headless参数。

前提条件:

- 本地安装Firefox浏览器

- 本地需要geckodriver驱动器文件,如果不配置环境变量的话,需要手动指定executable_path参数。

示例代码:

from selenium.webdriver import Firefox

from selenium.webdriver.firefox.options import Options

def main():

options=Options()

options.add_argument('-headless')

driver= Firefox(executable_path='./geckodriver', firefox_options=options)

driver.get("https://www.baidu.com/")

print(driver.page_source)

driver.close()if __name__ == '__main__':

main()

Selenium+Headless Chrome

前提条件:

- 本地安装Chrome浏览器

- 本地需要chromedriver驱动器文件,如果不配置环境变量的话,需要手动指定executable_path参数。

示例:

from selenium import webdriver

from selenium.webdriver.chrome.options import Options

def main():

chrome_options=Options()

chrome_options.add_argument('--headless')

chrome_options.add_argument('--disable-gpu')

driver= webdriver.Chrome(executable_path='./chromedriver', chrome_options=chrome_options)

driver.get("https://www.baidu.com")

print(driver.page_source)

driver.close()if __name__ == '__main__':

main()

python selenium模块使用出错,找不到路径

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

Windows环境下:

1、下载geckodriver.exe:

地址:https://github.com/mozilla/geckodriver/releases

请根据系统版本选择下载;(如Windows 64位系统)

2、下载解压后将getckodriver.exe复制到Firefox的安装目录下,如(C:\Program Files\Mozilla Firefox),并在环境变量Path中添加路径:C:\Program Files\Mozilla Firefox;

3.重启cmd或IDLE再次运行代码即可

ubuntu16.04环境下

1、下载 geckodriverckod

地址: https://github.com/mozilla/geckodriver/releases

2、解压后将geckodriverckod 存放至 /usr/local/bin/ 路径下即可

参考链接:

https://blog.csdn.net/liu5257/article/details/53437878

https://blog.csdn.net/u010358168/article/details/79749149

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值