cssやおそらくjsによって読み込まれた画像を含むすべての画像を取得するには、htmlコード以上のものが必要です。 が htmlとcssとjsを理解するコードが必要です。 フルブラウザが必要です。
幸い、Javaにはブラウザが付属しています。 JavaFXWebEngine。 URLまたはHTMLを指定すると、すべてがロードされます。 WebKitとして、最新の画像読み込み技術、例えばCSS border-imageを知っています。
画像を取得する方法が必要です。 それはmedia listを提供していませんが、それは純粋なJavaのであるから、私たちはその要求をインターセプトするためにJavaのURLハンドラをハイジャックすることができます
import java.io.IOException; import java.net.URL; import java.net.URLConnection; import javafx.application.Application; import javafx.application.Platform; import javafx.concurrent.Worker; import javafx.scene.Scene; import javafx.scene.web.WebView; import javafx.stage.Stage;
public class NetworkMonitor extends Application {
private final String url = "http://www.google.com/";
public static void main(String[] args) {
// Override default http/https handler. Must do once only.
URL.setURLStreamHandlerFactory(protocol ->
protocol.equals("http") ? new HttpHandler() :
protocol.equals("https") ? new HttpsHandler() : null);
// Launch as JavaFX app. Required for WebView/WebEngine.
launch(args);
}
@Override public void start(Stage primaryStage) throws Exception {
// Create webview and listen for ondone
WebView v = new WebView();
v.getEngine().getLoadWorker().stateProperty().addListener((prop, old, now) -> {
if (now == Worker.State.SUCCEEDED || now == Worker.State.FAILED)
Platform.exit(); });
// Showing GUI is easiest way to make sure ondone will be fired.
primaryStage.setScene(new Scene(v));
primaryStage.show();
// Load the target url.
v.getEngine().load(url);
}
// Your IDE should warn you about the sun package.
private static class HttpHandler extends sun.net.www.protocol.http.Handler {
@Override protected URLConnection openConnection(URL url) throws IOException {
System.out.println(url); // Capture url!
return super.openConnection(url);
}
}
// If there is no warning, you need to switch to a better IDE!
private static class HttpsHandler extends sun.net.www.protocol.https.Handler {
@Override protected URLConnection openConnection(URL url) throws IOException {
System.out.println(url); // Capture url!
return super.openConnection(url);
}
}
}
あなただけのURLを取得する方法を求めているので、これはコードが何をすべきかです。 必要に応じてコードを拡張することができます。
たとえば、2つのdecoratorのURLConnectionオブジェクトを使用すると、getInputStreamの呼び出しを傍受し、ヘッダー(MIMEタイプを判別)とストリームをフォーク(画像のコピーを保存)することができます。
この回答が役に立つ場合は、投票することを忘れないでください!
あなたの質問を明確にするための基本的なコードを提供してください –