现在我正在开发一个 GUI 项目,我正在尝试拍摄从网站源代码内部找到的 URL 中找到的照片,并将它们加载到我的 JavaFx GUI 中。 例如,我希望Java加载网站http://www.imdb.com/movies-in-theaters/?ref_=nv_tp_inth_1 ,并收集您向下滚动页面时看到的所有“封面照片”/缩略图(无论图像大小),然后将它们加载到 GUI View 中(作为一堆 ImageView 加载到一个 HBox 中)示例)。
也更深入,最终我想明白这一点,即用户可以单击图像/ ImageView ,并且(再次举例)它将显示所选电影的预告片。 (我的想法是,如果您点击进入下一页,找到链接,转到 YouTube,并删除除视频播放器之外的所有内容,则可以从网站找到预告片链接)。
在我使用的网络浏览器中,我可以查看页面的 HTML 元素/设计,并查看所有源代码。经过几次旋转后,我可以轻松找到我正在寻找的缩略图/图像的直接 URL,并且我发现在 javaFX 中我可以将图像作为 URL 加载到我的 GUI 中,如下所示:
Image img = new Image("http://website/websiteSubPage/websiteImage");
ImageView imgView = new ImageView(img);
我还发现我正在寻找的概念称为 WebScraping...但是到目前为止我所研究和研究的所有模块都无法满足我的需要。到目前为止我发现的最接近的模块是 HtmlUnit。然而,HtmlUnit 是关于 Web 自动化的——我在它的文档中找不到任何关于查找照片并将其加载为可调用到 ImageView 中的 Java Image 对象的内容。
目前我最好的猜测是让 Java 在后台加载网站,收集源代码,然后我可以创建一个字符串操纵器,它本质上只是查找、修剪并加载 URL它找到的每张图像,并将其放入充满 ImageView 的 HBox 中。
最终,我觉得我唯一的解决方案看起来像这样:
public HBox listView(){
HBox temp = new HBox();
// Load the website
// Load the source code into a large string.
for (int i=0; i>=<numberOfPhotosPreCalculatedSomeHow>;i++){
Image img = new Image( /*Manipulated string algorithm to find the next image URL*/);
ImageView imgView = new ImageView(img);
ImageView.setOnAction(e -> { /* load the trailer */ }; } // (Lambda)
temp.getChildren().add(ImageView);
}
return temp;
}
但是,做这一切...让我觉得我正在做一些非常错误的事情,我需要一些帮助。
想法?有没有专门为此构建的模块或插件?这可能吗,还是只是愚蠢?
最佳答案
找到答案了! 有内置的 java 方法可以让我从网站扫描信息,然后根据需要对其进行解密。
就我而言,这是我使用的代码:
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class WebReader extends Application{
// Class variable to hold our found URLs :)
static ArrayList<String> listArray;
public static void main(String[] args) throws IOException {
// Gather page & URL data, and read it
String address = "http://reddit.com";
URL pageLocation = new URL(address);
Scanner in = new Scanner(pageLocation.openStream());
// Initialize an ArrayList to store all of our collected URLs
listArray = new ArrayList<String>();
// Decipher the code line by line
while (in.hasNext()) {
String line = in.next();
if (line.contains("href=\"http://")) {
int from = line.indexOf("\"");
int to = line.lastIndexOf("\"");
System.out.println(line.substring(from + 1, to));
listArray.add(line.substring(from + 1, to));
}
}
// Next, we implement into JavaFx
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("My loaded photos");
// Create a place to put our content
HBox content = new HBox();
ScrollPane scrollPane = new ScrollPane(content);
scrollPane.setFitToHeight(true);
System.out.println(listArray.size());
for (int i = 0; i <= listArray.size() - 1; i++) {
Image img = new Image(listArray.get(i));
ImageView imgView = new ImageView(img);
content.getChildren().add(imgView);
} // Launch and sail away!! :)
Scene s = new Scene(scrollPane, 800, 600);
primaryStage.setScene(s);
primaryStage.show();
}
}
这就是我能够找到的解决方案 - 我不敢相信我花了这么长时间才找到解决方案,但我希望这对与我有相同想法的人有所帮助。 :)
关于java - 从网站拍摄照片并将其加载到 JavaFX 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37313834/