Skip to content

Commit

Permalink
通过kill杀死linux上残留的chrome进程,释放内存;修改example
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyong920 committed Jun 9, 2024
1 parent 9d24073 commit efc9a24
Show file tree
Hide file tree
Showing 39 changed files with 412 additions and 363 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ compile "io.github.fanyong920:jvppeteer:1.1.5"
```java
//设置基本的启动配置,这里选择了‘有头’模式启动
ArrayList<String> argList = new ArrayList<>();
//自动下载,第一次下载后不会再下载
BrowserFetcher.downloadIfNotExist(null);
LaunchOptions options = new LaunchOptionsBuilder().withArgs(argList).withHeadless(false).build();
//withExecutablePath 是指定chrome的启动路径
//chrome的启动路径可以从以下几方面之一去设置:
//1.直接指定。使用LaunchOptions的withExecutablePath方法
//2.配置环境变量。在Constant.EXECUTABLE_ENV可以找到配置环境变量的key
//3.使用BrowserFetcher.downloadIfNotExist()下载。如果使用使用BrowserFetcher.downloadIfNotExist()下载了chrome,在1和2选项都没有配置的情况下,才会使用下载的chrome路径;
//4.搜索电脑上采用默认安装的chrome路径。 1、2、3选项都没有配置的情况下,会自动搜索,此项不用任何操作
LaunchOptions options = new LaunchOptionsBuilder().withArgs(argList).withHeadless(false).withExecutablePath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe").build();
argList.add("--no-sandbox");
argList.add("--disable-setuid-sandbox");
Puppeteer.launch(options);
Expand All @@ -95,10 +99,9 @@ compile "io.github.fanyong920:jvppeteer:1.1.5"
#### 2、导航至某个页面

```java
//自动下载,第一次下载后不会再下载
BrowserFetcher.downloadIfNotExist(null);
//withExecutablePath 是指定chrome的启动路径
ArrayList<String> argList = new ArrayList<>();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(argList).withHeadless(false).build();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(argList).withExecutablePath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe").withHeadless(false).build();
argList.add("--no-sandbox");
argList.add("--disable-setuid-sandbox");
Browser browser = Puppeteer.launch(options);
Expand All @@ -116,11 +119,10 @@ compile "io.github.fanyong920:jvppeteer:1.1.5"
#### 3、生成页面的 PDF

```java
//自动下载,第一次下载后不会再下载
BrowserFetcher.downloadIfNotExist(null);

ArrayList<String> arrayList = new ArrayList<>();
//生成pdf必须在无厘头模式下才能生效
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(true).build();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(true).withExecutablePath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe").build();
arrayList.add("--no-sandbox");
arrayList.add("--disable-setuid-sandbox");
Browser browser = Puppeteer.launch(options);
Expand All @@ -138,8 +140,8 @@ compile "io.github.fanyong920:jvppeteer:1.1.5"
#### 4、TRACING 性能分析

```java
//自动下载,第一次下载后不会再下载
BrowserFetcher.downloadIfNotExist(null);

//即使不指定chrome路径,程序也会寻找默认安装chrome的路径,如果安chrome修改了默认路径,则寻找不到chrome
ArrayList<String> argList = new ArrayList<>();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(argList).withHeadless(true).build();
argList.add("--no-sandbox");
Expand Down
3 changes: 3 additions & 0 deletions example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,7 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.ruiyun.jvppeteer.options.PageNavigateOptions;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
Expand All @@ -18,8 +17,9 @@
public class AutoCommentExample {

public static void main(String[] args) throws Exception {
//自动下载,第一次下载后不会再下载
//自动下载,第一次下载后不会再下载,下载到默认路径,如果电脑上已经有chrome了,就不需要执行这行代码。自行下载内置的下载链接可能失效,建议自行下载安装chrome,然后指定路径启动chrome
BrowserFetcher.downloadIfNotExist(null);
//指定chrome启动路径
Browser browser = lauch("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
Page page = browser.newPage();
PageNavigateOptions navigateOptions = new PageNavigateOptions();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.ruiyun.example;

import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.BrowserFetcher;
import com.ruiyun.jvppeteer.options.LaunchOptions;
import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder;
import com.ruiyun.jvppeteer.core.browser.Browser;
Expand All @@ -15,13 +14,12 @@ public static void main(String[] args) throws Exception {

ArrayList<Browser> browsers = new ArrayList<>();
for (int i = 0; i < 5; i++) {
//ExecutablePath是指定chrome启动路径
ArrayList<String> arrayList = new ArrayList<>();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false)/*.withExecutablePath(path)*/.build();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false).withExecutablePath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe").build();
arrayList.add("--no-sandbox");
arrayList.add("--disable-setuid-sandbox");

//自动下载,第一次下载后不会再下载
BrowserFetcher.downloadIfNotExist(null);

Browser browser = Puppeteer.launch(options);
browsers.add(browser);
Expand Down
15 changes: 4 additions & 11 deletions example/src/main/java/com/ruiyun/example/BrowserEventExample.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.ruiyun.example;

import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.BrowserFetcher;
import com.ruiyun.jvppeteer.options.LaunchOptions;
import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder;
import com.ruiyun.jvppeteer.core.browser.Browser;
Expand All @@ -11,20 +10,14 @@

public class BrowserEventExample {
public static void main(String[] args) throws Exception {
//自动下载,第一次下载后不会再下载
BrowserFetcher.downloadIfNotExist(null);

//ExecutablePath是指定chrome启动路径
ArrayList<String> arrayList = new ArrayList<>();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false).build();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withExecutablePath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe").withHeadless(false).build();
arrayList.add("--no-sandbox");
arrayList.add("--disable-setuid-sandbox");
Browser browser = Puppeteer.launch(options);
browser.onDisconnected((s) ->{
System.out.println("浏览器断开连接");
});
browser.onTargetcreated((target) -> {
System.out.println("target type: "+target.type());
});
browser.onDisconnected((s) -> System.out.println("浏览器断开连接"));
browser.onTargetcreated((target) -> System.out.println("target type: "+target.type()));
Page page = browser.newPage();
page.goTo("https://www.baidu.com/?tn=98012088_10_dg&ch=3");
browser.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.ruiyun.example;

import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.BrowserFetcher;
import com.ruiyun.jvppeteer.options.BrowserOptions;
import com.ruiyun.jvppeteer.options.LaunchOptions;
import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder;
import com.ruiyun.jvppeteer.core.browser.Browser;
Expand All @@ -14,14 +12,12 @@

public class BrowserFunctionExample {
public static void main(String[] args) throws Exception {
//自动下载,第一次下载后不会再下载
BrowserFetcher.downloadIfNotExist(null);
//ExecutablePath是指定chrome启动路径
ArrayList<String> arrayList = new ArrayList<>();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false).build();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false).withExecutablePath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe").build();
arrayList.add("--no-sandbox");
arrayList.add("--disable-setuid-sandbox");
Browser browser = Puppeteer.launch(options);
Browser browser1 = Puppeteer.launch(options);

System.out.println("defaultBrowserContext: "+browser.defaultBrowserContext());
String wsEndpoint = browser.wsEndpoint();
Expand All @@ -38,9 +34,7 @@ public static void main(String[] args) throws Exception {
if(StringUtil.isNotEmpty(browserContext.getId()))
browser.disposeContext(browserContext.getId());
}
browser.onDisconnected((s) -> {
System.out.println("我是浏览器事件监听,现在监听到 disconnected");
});
browser.onDisconnected((s) -> System.out.println("我是浏览器事件监听,现在监听到 disconnected"));
browser.disconnect();
System.out.println("isConnected: "+ browser.isConnected());

Expand Down
7 changes: 3 additions & 4 deletions example/src/main/java/com/ruiyun/example/CoverageExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.Browser;
import com.ruiyun.jvppeteer.core.browser.BrowserFetcher;
import com.ruiyun.jvppeteer.core.page.Page;
import com.ruiyun.jvppeteer.options.LaunchOptions;
import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder;
import com.ruiyun.jvppeteer.protocol.profiler.CoverageEntry;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CoverageExample {
public static void main(String[] args) throws Exception {
//自动下载,第一次下载后不会再下载
BrowserFetcher.downloadIfNotExist(null);
LaunchOptions launchOptions = new LaunchOptionsBuilder().withIgnoreDefaultArgs(Arrays.asList("--enable-automation")).withHeadless(false).build();
//ExecutablePath是指定chrome启动路径
LaunchOptions launchOptions = new LaunchOptionsBuilder().withIgnoreDefaultArgs(Collections.singletonList("--enable-automation")).withHeadless(false).withExecutablePath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe").build();
Browser browser = Puppeteer.launch(launchOptions);
Page page = browser.newPage();
page.coverage().startJSCoverage();//会导致不能页面不能加载完成
Expand Down
11 changes: 4 additions & 7 deletions example/src/main/java/com/ruiyun/example/CrawlerTaobaoItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.ruiyun.jvppeteer.core.Constant;
import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.Browser;
import com.ruiyun.jvppeteer.core.browser.BrowserFetcher;
import com.ruiyun.jvppeteer.core.page.Page;
import com.ruiyun.jvppeteer.options.LaunchOptions;
import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder;
Expand Down Expand Up @@ -39,11 +38,9 @@

public class CrawlerTaobaoItem {
public static void main(String[] args) throws Exception {
//自动下载,第一次下载后不会再下载
BrowserFetcher.downloadIfNotExist(null);
//指定启动路径,启动浏览器
//ExecutablePath是指定chrome启动路径
ArrayList<String> argList = new ArrayList<>();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(argList).withHeadless(false).build();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(argList).withHeadless(false).withExecutablePath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe").build();
argList.add("--no-sandbox");
argList.add("--disable-setuid-sandbox");
Browser browser = Puppeteer.launch(options);
Expand Down Expand Up @@ -432,7 +429,7 @@ private Object parseItem(String content) throws ParserException {
}
}

/**C、商品卖点 */
/*C、商品卖点 */
Pattern start = Pattern.compile("<p class=\"tb-subtitle\">.*?</p>");
Matcher ms = start.matcher(resBuffer.toString());
String subTitleText = null;
Expand Down Expand Up @@ -532,7 +529,7 @@ private Object parseItem(String content) throws ParserException {
}
itemImgs.add(imgUrl);
}
if (itemImgs.size() > 0) {
if (!itemImgs.isEmpty()) {
try {
itemImgStr = Constant.OBJECTMAPPER.writeValueAsString(itemImgs);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.Browser;
import com.ruiyun.jvppeteer.core.browser.BrowserFetcher;
import com.ruiyun.jvppeteer.options.LaunchOptions;
import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder;
import com.ruiyun.jvppeteer.core.page.Page;
Expand All @@ -12,10 +11,9 @@
public class CreatNewPageExample {

public static void main(String[] args) throws Exception {
//自动下载,第一次下载后不会再下载
BrowserFetcher.downloadIfNotExist(null);

ArrayList<String> arrayList = new ArrayList<>();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false).build();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false).withExecutablePath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe").build();
arrayList.add("--no-sandbox");
arrayList.add("--disable-setuid-sandbox");
Browser browser = Puppeteer.launch(options);
Expand Down
6 changes: 2 additions & 4 deletions example/src/main/java/com/ruiyun/example/LaunchExample.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.ruiyun.example;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

Expand All @@ -19,9 +17,9 @@ public class LaunchExample {

@Test
public void test1() throws Exception {
//自动下载722234版本的浏览器,第一次下载后不会再下载
//自动下载722234版本的浏览器,第一次下载后不会再下载,内置有下载链接,下载链接可能失效。
BrowserFetcher.downloadIfNotExist();
LaunchOptions launchOptions = new LaunchOptionsBuilder().withIgnoreDefaultArgs(Arrays.asList("--enable-automation")).withHeadless(false).build();
LaunchOptions launchOptions = new LaunchOptionsBuilder().withIgnoreDefaultArgs(Collections.singletonList("--enable-automation")).withHeadless(false).build();
Browser browser = Puppeteer.launch(launchOptions);
Page page = browser.newPage();
page.goTo("https://www.baidu.com/?tn=98012088_10_dg&ch=3");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.Browser;
import com.ruiyun.jvppeteer.core.browser.BrowserFetcher;
import com.ruiyun.jvppeteer.core.page.ElementHandle;
import com.ruiyun.jvppeteer.core.page.Page;
import com.ruiyun.jvppeteer.options.Clip;
Expand All @@ -16,10 +15,9 @@ public class MouseWheelExample {

@Test
public void test1() throws Exception {
//自动下载,第一次下载后不会再下载
BrowserFetcher.downloadIfNotExist(null);

ArrayList<String> arrayList = new ArrayList<>();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false).build();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false).withExecutablePath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe").build();
arrayList.add("--no-sandbox");
arrayList.add("--disable-setuid-sandbox");
Browser browser = Puppeteer.launch(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class OnTargetCreatePage {
public static void main(String[] args) throws Exception {
BrowserFetcher.downloadIfNotExist(null);
ArrayList<String> arrayList = new ArrayList<>();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false).build();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false).withExecutablePath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe").build();
arrayList.add("--no-sandbox");
arrayList.add("--disable-setuid-sandbox");
Browser browser = Puppeteer.launch(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.Browser;
import com.ruiyun.jvppeteer.core.browser.BrowserFetcher;
import com.ruiyun.jvppeteer.core.page.Page;
import com.ruiyun.jvppeteer.options.LaunchOptions;
import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder;
Expand All @@ -17,9 +16,8 @@ public class PageAddScriptExample {
public static void main(String[] args) throws Exception {
// 注入jquery https://cdn.bootcss.com/jquery/3.4.1/jquery.js
ArrayList<String> arrayList = new ArrayList<>();
//自动下载,第一次下载后不会再下载
BrowserFetcher.downloadIfNotExist(null);
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false).build();

LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false).withExecutablePath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe").build();
arrayList.add("--no-sandbox");
arrayList.add("--disable-setuid-sandbox");
Browser browser = Puppeteer.launch(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void main(String[] args) throws Exception {
BrowserFetcher.downloadIfNotExist(null);

ArrayList<String> arrayList = new ArrayList<>();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false).build();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false).withExecutablePath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe").build();
arrayList.add("--no-sandbox");
arrayList.add("--disable-setuid-sandbox");
Browser browser = Puppeteer.launch(options);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.ruiyun.example;

import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.BrowserFetcher;
import com.ruiyun.jvppeteer.options.Device;
import com.ruiyun.jvppeteer.options.LaunchOptions;
import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder;
import com.ruiyun.jvppeteer.core.browser.Browser;
Expand All @@ -16,10 +14,9 @@ public class PageContentExample {

public static void main(String[] args) throws InterruptedException, IOException, ExecutionException {

BrowserFetcher.downloadIfNotExist(null);

ArrayList<String> arrayList = new ArrayList<>();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false).build();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false).withExecutablePath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe").build();
arrayList.add("--no-sandbox");
arrayList.add("--disable-setuid-sandbox");
Browser browser = Puppeteer.launch(options);
Expand Down
Loading

0 comments on commit efc9a24

Please sign in to comment.