- 依赖
<dependency>
<groupId>io.github.fanyong920</groupId>
<artifactId>jvppeteer</artifactId>
<version>1.1.5</version>
</dependency>
- 工具
import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.Browser;
import com.ruiyun.jvppeteer.core.page.Page;
import com.ruiyun.jvppeteer.options.LaunchOptions;
import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder;
import com.ruiyun.jvppeteer.options.Viewport;
import com.ruiyun.jvppeteer.protocol.network.Cookie;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
public class ChromeUtil {
@Getter
private static Browser browser;
private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36";
private static final String EXE_PATH = "C:\\Users\\xiaog\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe";
@Getter
private static String cookie;
public static void openBrowser() {
try {
String exePath = isWindows() ? EXE_PATH : "/usr/bin/google-chrome";
ArrayList<String> argList = new ArrayList<>();
Viewport viewport = new Viewport();
viewport.setWidth(1920);
viewport.setHeight(1024);
viewport.setHasTouch(true);
argList.add("--no-sandbox");
argList.add("--disable-setuid-sandbox");
argList.add("--disable-infobars");
// argList.add("--incognito"); // 无痕模式
argList.add("--disable-gpu");
argList.add("--disable-web-security");
argList.add("--disable-dev-shm-usage");
argList.add("--ignore-certificate-errors");
argList.add("--allow-running-insecure-content");
argList.add("--mute-audio");
argList.add("--start-maximized");
LaunchOptions options = new LaunchOptionsBuilder()
.withArgs(argList)
.withHeadless(true)
.withViewport(viewport)
.withIgnoreDefaultArgs(Arrays.asList("--enable-automation"))
.withIgnoreHTTPSErrors(true)
.withExecutablePath(exePath)
.build();
browser = Puppeteer.launch(options);
} catch (Exception e) {
e.printStackTrace();
}
}
private static boolean isWindows() {
String osName = System.getProperty("os.name").toLowerCase();
return osName.contains("windows");
}
public static void closeBrowser() {
if (browser != null) {
try {
browser.close();
}catch (Exception e) {
e.printStackTrace();
}
}
/*try {
if (isWindows()) {
// 执行脚本
Process process = Runtime.getRuntime().exec("taskkill -F /fi \"imagename eq chrome.exe\"");
// 可选:等待脚本执行完成
int exitCode = process.waitFor();
log.info("exit code:" + exitCode);
}else {
// 执行脚本
Process process = Runtime.getRuntime().exec("pkill -f \"(chrome)\"");
// 可选:等待脚本执行完成
int exitCode = process.waitFor();
log.info("exit code:" + exitCode);
}
} catch (Exception e) {
e.printStackTrace();
}*/
}
private static void preHandler(Page page) {
page.setUserAgent(USER_AGENT);
page.setCacheEnabled(true);
page.evaluateOnNewDocument("() => {const newProto = navigator.__proto__;delete newProto.webdriver; navigator.__proto__ = newProto;}");
page.evaluateOnNewDocument("() => {window.chrome = {};window.chrome.app = {InstallState: 'hehe',RunningState: 'haha',getDetails: 'xixi',getIsInstalled: 'ohno',};window.chrome.csi = function () {};window.chrome.loadTimes = function () {};window.chrome.runtime = function () {};}");
page.evaluateOnNewDocument("() => {Object.defineProperty(navigator, 'userAgent', {get: () =>'"+USER_AGENT+"',});}");
page.evaluateOnNewDocument("() => {Object.defineProperties(navigator,{ webdriver:{ get: () => undefined } }) }");
page.evaluateOnNewDocument("() => {Object.defineProperty(navigator, 'plugins', {get: () => [{0: {type: 'application/x-google-chrome-pdf',suffixes: 'pdf',description: 'Portable Document Format',enabledPlugin: Plugin,},description: 'Portable Document Format',filename: 'internal-pdf-viewer',length: 1,name: 'Chrome PDF Plugin',},{0: {type: 'application/pdf',suffixes: 'pdf',description: '',enabledPlugin: Plugin,},description: '',filename: 'mhjfbmdgcfjbbpaeojofohoefgiehjai',length: 1,name: 'Chrome PDF Viewer',},{0: {type: 'application/x-nacl',suffixes: '',description: 'Native Client Executable',enabledPlugin: Plugin,},1: {type: 'application/x-pnacl'," +
"suffixes: '',description: 'Portable Native Client Executable',enabledPlugin: Plugin,},description: '',filename: 'internal-nacl-plugin',length: 2,name: 'Native Client',},],});}");
page.evaluateOnNewDocument("() => {const originalQuery = window.navigator.permissions.query; window.navigator.permissions.query = (parameters) =>parameters.name === 'notifications' ? Promise.resolve({ state: Notification.permission }) : originalQuery(parameters);}");
page.evaluateOnNewDocument("() => {const getParameter = WebGLRenderingContext.getParameter;WebGLRenderingContext.prototype.getParameter = function (parameter) {if (parameter === 37445) {return 'Intel Inc.';}if (parameter === 37446) {return 'Intel(R) Iris(TM) Graphics 6100';}return getParameter(parameter);};}");
page.evaluateOnNewDocument("() =>{ Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] }); }");
}
public static String getPage(String url) {
String html = null;
Page page = null;
try {
page = browser.newPage();
preHandler(page);
page.goTo(url);
setCookie(page.cookies());
html = page.content();
}catch (Exception e) {
e.printStackTrace();
}finally {
if (page != null) {
try {
page.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return html;
}
public static String getPage(String url,long delay) {
String html = null;
Page page = null;
try {
page = browser.newPage();
preHandler(page);
page.goTo(url);
Thread.sleep(delay);
page.evaluate("() => window.stop()");
setCookie(page.cookies());
html = page.content();
}catch (Exception e) {
e.printStackTrace();
}finally {
if (page != null) {
try {
page.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return html;
}
public static void setCookie(List<Cookie> cookieList) {
cookie = cookieList.stream()
.map(x -> x.getName()+"="+x.getValue())
.collect(Collectors.joining("; "));
}
public static String getPage(String url,String waitElementSelector) {
String html = null;
Page page = null;
try {
page = browser.newPage();
preHandler(page);
page.goTo(url);
page.waitForSelector(waitElementSelector);
setCookie(page.cookies());
html = page.content();
}catch (Exception e) {
e.printStackTrace();
}finally {
if (page != null) {
try {
page.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return html;
}
public static String getPageByScroll(String url,int delay,int scrollNum,long scrollDelay) {
String html = null;
Page page = null;
try {
page = browser.newPage();
preHandler(page);
page.goTo(url);
Thread.sleep(delay);
setCookie(page.cookies());
long half = scrollDelay / 2;
for (int i = 0; i < scrollNum; i++) {
Thread.sleep(half);
page.evaluate("() => { window.scrollTo(0,document.body.scrollHeight); }");
Thread.sleep(half);
page.evaluate("() => { window.scrollTo(0,0); }");
}
html = page.content();
}catch (Exception e) {
e.printStackTrace();
}finally {
if (page != null) {
try {
page.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return html;
}
public static String getPageByScroll(String url,int scrollNum,long scrollDelay,String waitElementSelector) {
String html = null;
Page page = null;
try {
page = browser.newPage();
preHandler(page);
page.goTo(url);
page.waitForSelector(waitElementSelector);
setCookie(page.cookies());
long half = scrollDelay / 2;
for (int i = 0; i < scrollNum; i++) {
Thread.sleep(half);
page.evaluate("() => { window.scrollTo(0,document.body.scrollHeight); }");
Thread.sleep(half);
page.evaluate("() => { window.scrollTo(0,0); }");
}
html = page.content();
}catch (Exception e) {
e.printStackTrace();
}finally {
if (page != null) {
try {
page.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return html;
}
public static Object executeJs(String jsCode) {
try {
Page page = browser.pages().get(0);
return page.evaluate(jsCode);
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
标签:String,try,html,static,jvppeteer,工具,null,page
From: https://www.cnblogs.com/xiaogblog/p/18163867