0%

Centos7-Selenium-Java-Programming-Environment

最近在编写爬虫程序,这里简单的做下笔记。

阅读注意事项

  1. 需要一台服务器(阿里ESC)这种,分配的公网ip。
  2. 这里使用的yum安装,所以环境配置都创建好了,不需要在手动修改。

准备工作

安装Java、maven、git、selenium运行环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 使用的机器是centos7.4
# 系统环境一个一个来安装。
# 1.检查是否安装jdk
-> yum install java-1.8.0-openjdk-devel.x86_64
-> java -version
openjdk version "1.8.0_262"
OpenJDK Runtime Environment (build 1.8.0_262-b10)
OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode)
-> yum install maven
-> mvn -version
Java version: 1.8.0_262, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-0.el7_8.x86_64/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-862.el7.x86_64", arch: "amd64", family: "unix"
-> yum install git
git version 1.8.3.1
# 查看是否生成过证书
-> ls -al ~/.ssh
# 这里配置下ssh公钥方便git拉去代码
-> ssh-keygen -t rsa -C “you email@gamil.com”
# 查看证书
-> cat ~/.ssh/id_rsa.pub
# 开始配置 selenium 运行环境
-> wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
-> yum localinstall google-chrome-stable_current_x86_64.rpm
# 检查版本
-> google-chrome --version
Google Chrome 85.0.4183.83
# 安装 chromedriver
-> yum install chromedriver
-> chromedriver -version
ChromeDriver 84.0.4147.89
# 这里注意 chromedriver 可能与chrome的版本不一致,去官方网站下载后解压复制到/usr/bin中
# 下载地址 https://chromedriver.chromium.org/
  • 注意 google-chrome 和 chromedriver版本是需要对应的具体可以到官方查看。
  • 在centos上运行需要配置下关键参数
1
2
3
4
5
6
7
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--disable-gpu"); // 不配置一定报错
chromeOptions.addArguments("--no-sandbox");
// 创建无界面浏览器对象
chromeOptions.setHeadless(true);
return new ChromeDriver(chromeOptions);
  • 启动代码的需要指定环境变量
1
2
System.setProperty("webdriver.chrome.chromedriver", "/usr/bin/chromedriver");
# 具体可以通过 whereis chromedriver查看。

参考文献

CentOS使用yum安装jdk