Playwright防检测

/ 后端 / 没有评论 / 431浏览

防检测脚本: stealth.min.js

package com.kakuiwong;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.locks.LockSupport;

public class App {
    public static void main(String[] args) throws IOException {
        String read = read();

        try (Playwright playwright = Playwright.create()) {
            BrowserType.LaunchOptions launchOptions = new BrowserType.LaunchOptions();
            launchOptions.setChannel("chrome");
            launchOptions.setHeadless(false);
            Browser browser = playwright.chromium().launch(launchOptions);
            Page page = browser.newPage();
            page.addInitScript(read);
            page.navigate("https://bot.sannysoft.com/");
            System.out.println(page.title());
            LockSupport.park();
        }
    }

    public static String read() throws IOException {
        InputStream resourceAsStream = App.class.getClassLoader().getResourceAsStream("site-digger.com_uploads_stealth.min.js");
        int n = 0;
        byte[] buffer = new byte[4096];
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        while (-1 != (n = resourceAsStream.read(buffer))) {
            output.write(buffer, 0, n);
        }
        output.flush();
        String text = output.toString();
        resourceAsStream.close();
        output.close();
        return text;
    }
}