117 lines
3.7 KiB
Java
117 lines
3.7 KiB
Java
package com.yutou.qqbot.utlis;
|
|
|
|
import com.alibaba.fastjson2.JSONArray;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import org.openqa.selenium.Cookie;
|
|
import org.openqa.selenium.Proxy;
|
|
import org.openqa.selenium.WebDriver;
|
|
import org.openqa.selenium.chrome.ChromeDriver;
|
|
import org.openqa.selenium.chrome.ChromeOptions;
|
|
import org.openqa.selenium.remote.CapabilityType;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
public class WebClient {
|
|
private WebDriver driver;
|
|
private static WebClient instance;
|
|
|
|
public static WebClient getInstance() {
|
|
if (instance == null || instance.driver == null) {
|
|
instance = new WebClient();
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
public WebDriver getWebDriver() {
|
|
if (driver == null) {
|
|
driver = new ChromeDriver(getOptions());
|
|
}
|
|
return driver;
|
|
}
|
|
|
|
public void quit() {
|
|
driver.quit();
|
|
driver = null;
|
|
}
|
|
|
|
private WebClient() {
|
|
//System.setProperty("webdriver.http.factory", "jdk-http-client");
|
|
System.setProperty("webdriver.chrome.driver",
|
|
ConfigTools.load(ConfigTools.CONFIG, "chrome", String.class));
|
|
// System.setProperty("webdriver.chrome.whitelistedIps", "");
|
|
// java.util.logging.Logger.getLogger("org.openqa.selenium").setLevel(Level.OFF);
|
|
}
|
|
|
|
|
|
public static List<Cookie> loadCookie(JSONArray array) {
|
|
List<Cookie> list = new ArrayList<>();
|
|
|
|
for (Object o : array) {
|
|
JSONObject json = (JSONObject) o;
|
|
boolean containsDate = json.containsKey("expirationDate");
|
|
long t = 0;
|
|
if (containsDate) {
|
|
String _time = json.getString("expirationDate");
|
|
if (_time.contains(".")) {
|
|
_time = _time.split("\\.")[0];
|
|
}
|
|
t = Long.parseLong(_time);
|
|
} else {
|
|
t = (System.currentTimeMillis()) / 1000;
|
|
}
|
|
t *= 1000;
|
|
|
|
Cookie cookie = new Cookie(json.getString("name"),
|
|
json.getString("value"),
|
|
json.getString("domain"),
|
|
json.getString("path"),
|
|
new Date(t),
|
|
json.getBooleanValue("secure"),
|
|
json.getBooleanValue("httpOnly")
|
|
|
|
);
|
|
list.add(cookie);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
static boolean headless = false;
|
|
|
|
public void setHeadless(boolean headless) {
|
|
WebClient.headless = headless;
|
|
}
|
|
|
|
public ChromeOptions getOptions() {
|
|
ChromeOptions options = new ChromeOptions();
|
|
options.addArguments("--remote-allow-origins=*");
|
|
// options.addArguments("--disable-gpu");
|
|
// options.addArguments("blink-settings=imagesEnabled=false");
|
|
String headless = RedisTools.get("chromedrive_headless");
|
|
String proxy = RedisTools.get("chromedrive_proxy");
|
|
if ("true".equals(proxy)) {
|
|
String url = "http://127.0.0.1:7890";
|
|
Proxy _proxy = new Proxy();
|
|
_proxy.setHttpProxy(url);
|
|
_proxy.setSslProxy(url);
|
|
options.setCapability(CapabilityType.PROXY, _proxy);
|
|
}
|
|
if ("true".equals(headless) || WebClient.headless) {
|
|
options.addArguments("--headless");
|
|
}
|
|
options.addArguments("--no-sandbox");
|
|
// options.addArguments("--incognito");
|
|
options.addArguments("--disable-plugins");
|
|
options.addArguments("--lang=zh-CN");
|
|
|
|
return options;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
WebDriver driver1 = getInstance().getWebDriver();
|
|
|
|
driver1.get("https://www.tsdm39.net/forum.php");
|
|
}
|
|
}
|