<dependency>
<groupId>io.github.fanyong920</groupId>
<artifactId>jvppeteer</artifactId>
<version>1.1.5</version>
</dependency>
<!-- 爬虫 -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
<!-- json解析工具 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
```java
//参考地址
Chrome命令行启动参数:https://peter.sh/experiments/chromium-command-line-switches/
DevTools Protocol:https://chromedevtools.github.io/devtools-protocol/
Puppeteer中文文档:https://zhaoqize.github.io/puppeteer-api-zh_CN/#/
```java
//设置基本的启动配置,这里选择了‘有头’模式启动
ArrayList<String> argList = new ArrayList<>();
argList.add("--no-sandbox");
argList.add("--disable-setuid-sandbox");
argList.add("--ignore-certificate-errors");//关闭chrome提示的(浏览器访问https提示“您的连接不是私密连接”的问题)
//设置浏览器启动的忽略参数
ArrayList<String> ignoreList = new ArrayList<>();
ignoreList.add("--enable-automation");//关闭chrome提示的(正在受自动化测试软件控制)
//自动下载,第一次下载后不会再下载
String projectPath = System.getProperty("user.dir");
String replace = projectPath.replace("\\", "/");
String path=replace+"/chrome-win/chrome.exe";
LaunchOptions options = new LaunchOptionsBuilder().withArgs(argList).withIgnoreDefaultArgs(ignoreList).withExecutablePath(path).withHeadless(false).build();
Browser browser = Puppeteer.launch(options);
Page page = browser.newPage();
//设置反爬机制
// page.evaluateOnNewDocument("() =>{ Object.defineProperties(navigator,{ webdriver:{ get: () => false } }) }");
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: () => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36', });}");
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',\n" +
" 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); };}");
Viewport viewport = new Viewport();
viewport.setWidth(1920);
viewport.setHeight(1024);
viewport.setHasTouch(true);
page.setViewport(viewport);
page.go("url");
page.content();//内容
Document parse = Jsoup.parse(page.content(), "utf-8");
page.mouse().move(689, 540);
page.mouse().down();
page.mouse().up();
page.waitForSelector("#proceed-link").click();//等待加载完毕
page.evaluate("document.querySelector('.css-field-search').value = '';");//输入js脚本清空某个元素的值
page.type(".css-field-search", tempPhone, 0);//输入值 0表示输入的速度最快
jvppeteer
于 2024-03-22 20:28:46 首次发布