返回
热门搜索

在linux系统下部署selenium爬虫程序介绍

IT博客 网络 tcp/ip 点击量 618

基本信息 收藏 - 举报 - 海报

详细介绍

目录
前言一、selenium是什么?二、使用步骤1.引入库2.测试代码三、部署程序1.安装chrome2.安装chromedriver驱动3.运行测试代码总结

前言

我这里是工作需要把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 一下看看

看到这个我么就算请求成功了

总结

到此这篇关于在linux系统下部署selenium爬虫程序介绍的文章就介绍到这了,更多相关linux selenium爬虫程序内容请搜索IT博客社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT博客社区!

没有更多内容。

用户评价(0)

好评度100%
  • 还没有人评论此条信息!
+ 加载更多

联系方式

提示:联系我时,请说明在看到的,谢谢!
  • 联系人:
  • 地  区:
  • 电  话: 共发布信息(2004)条 所在地:未填写
看了又看
加载中
首页 首页 收藏 收藏

电话联系