记一下mac m1上使用selenium的场景

已经有几年不做爬虫了 ,今天拿出了之前的一个项目。
闲来无事,就试试再mac m1上跑起来~,结果发现,都适配的很好了!
记一下我的步骤:

1:安装python包

# 我先创建了个虚拟环境
conda create -n selenium python=3.7
# 然后安装包
pip3 install selenium
pip3 install lxml
pip3 install requests
# 一定注意:如果你没有新建环境的话,一定注意安装过程中的包的版本是否适配,
# 如不适配,按照提示 pip3 install --upgrade XXXX==版本号

2:查看google chrome版本,下载chromedriver

我用的当前最新版本-95.0.4638.69
在这里插入图片描述
chromedriver下载地址:
http://chromedriver.storage.googleapis.com/index.html

在里面搜到自己的版本号进入:
在这里插入图片描述
找m1的包下载

3:跑起来~

option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
# option.add_argument('--headless')
option.add_argument('--disable-extensions')
option.add_argument('--headless')
option.add_argument('--disable-gpu')
option.add_argument('--no-sandbox')
option.add_argument('user-agent=' + UserAgent(path='./agent.json').random)
# 这里我指定了 chromedriver 的路径,然后 直接就可以用了~
browser = webdriver.Chrome(executable_path='./chromedriver', options=option)

4:把chromedriver放在默认目录下

默认目录:/usr/local/bin/
注意:/usr/bin/ 目录也可以,但是mac下该目录有很高的权限限制,不建议

# 注意:
sudo mv ./chromedriver /usr/local/bin/

验证是否成功:chromedriver -v
在这里插入图片描述
这样就不用指定Chromedriver目录了

# browser = webdriver.Chrome(executable_path='./chromedriver', options=option)
browser = webdriver.Chrome(options=option)
在Linux上使用selenium截图,需要安装selenium和对应的浏览器驱动。以使用Chrome浏览器为例,可以按照以下步骤操作: 1. 安装Chrome浏览器和ChromeDriver驱动: ``` # 安装Chrome浏览器 sudo apt-get update sudo apt-get install -y google-chrome-stable # 下载对应版本的ChromeDriver驱动并解压 wget https://chromedriver.storage.googleapis.com/88.0.4324.96/chromedriver_linux64.zip unzip chromedriver_linux64.zip ``` 2. 安装selenium: ``` pip install selenium ``` 3. 编写Python脚本,使用selenium打开网页并截图: ```python from selenium import webdriver # 创建Chrome浏览器实例 driver = webdriver.Chrome('./chromedriver') # 打开网页 driver.get('https://www.baidu.com') # 截图并保存 driver.save_screenshot('baidu.png') # 关闭浏览器 driver.quit() ``` 在Docker中使用selenium实现截图,可以使用selenium/standalone-chrome镜像,该镜像已经集成了Chrome浏览器和ChromeDriver驱动,可以直接使用。按照以下步骤操作: 1. 拉取selenium/standalone-chrome镜像: ``` docker pull selenium/standalone-chrome ``` 2. 运行容器: ``` docker run -d -p 4444:4444 selenium/standalone-chrome ``` 3. 在容器中执行Python脚本,使用selenium打开网页并截图: ```python from selenium import webdriver # 创建Chrome浏览器实例 driver = webdriver.Remote('http://localhost:4444/wd/hub', desired_capabilities=webdriver.DesiredCapabilities.CHROME) # 打开网页 driver.get('https://www.baidu.com') # 截图并保存 driver.save_screenshot('baidu.png') # 关闭浏览器 driver.quit() ``` 注意,在容器中执行Python脚本时,需要将Chrome浏览器实例的地址指定为http://localhost:4444/wd/hub。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值