linux下部署selenium爬虫程序


前言

我这里是工作需要把selenium 爬虫程序部署到Linux 服务器上面 顺便跟大家交流一下 如果有兴趣的话可以看一下,


一、selenium是什么?

Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样, 爬虫用它来抓取一些js动态加载的数据

二、使用步骤

1.引入库

代码如下

from selenium.webdriver import Chrome
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options  # 使用无头浏览器
from selenium.webdriver import ChromeOptions
chrome_options = Options()
options = ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation']) # =>去掉浏览器正在受到自动测试软件的控制
options.add_experimental_option('useAutomationExtension', False)
chrome_options.add_argument("--headless")  # => 为Chrome配置无头模式
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')

2.测试代码

代码如下:

s = Service(r"/home/driver/chromedriver")
driver = Chrome(
     service=s, options=chrome_options
 )
 driver.get("https://www.baidu.com")
 print(diiver.title)

三、部署程序

1.安装chrome

命令如下:

yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
检查chrome的版本
google-chrome --version

2.安装chromedriver驱动

命令如下:

按照对应的chrome版本下载chromedriver驱动
地址: https://npm.taobao.org/mirrors/chromedriver
我的版本号是: 96.0.4664.45
wget https://npm.taobao.org/mirrors/chromedriver/96.0.4664.45/chromedriver_linux64.zip 
yum install -y unzip zip
unzip chromedriver_linux64.zip # 解压zip文件
mkdir driver #新建文件夹用来存放驱动
chmod 777 driver/chromedriver # 这是权限 我这里给的是 777

3.运行测试代码

新建test.py文件

vi test.py

在这里插入图片描述
保存 test.py run 一下看看
在这里插入图片描述
看到这个我么就算请求成功了

总结

以上就是今天要讲的内容,本文仅仅简单介绍了selenium的使用,和 在linux下部署selenium的方法, 如果还有不明白的地方记得给我留言哦
  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

崔子末

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值