gpt4 book ai didi

java - 如何从网页中抓取图像?

转载 作者:行者123 更新时间:2023-12-01 19:05:58 26 4
gpt4 key购买 nike

我使用 htmlunit 从网页中抓取图像。我是 htmlunit 的初学者。我编码,但不知道如何获取图像。下面是我的代码。

import java.io.*;
import java.net.URL;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class urlscrap {

public static void main(String[] args) throws Exception
{

//WebClient webClient = new WebClient(Opera);
WebClient webClient = new WebClient();
HtmlPage currentPage = (HtmlPage) webClient.getPage(new URL("http://www.google.com"));

System.out.println(currentPage.asText());
//webClient.closeAllWindows();

}
}

最佳答案

这对你有用吗?

import java.net.URL;
import java.util.List;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class urlscrap {

public static void main(String[] args) throws Exception
{

//WebClient webClient = new WebClient(Opera);
WebClient webClient = new WebClient();
HtmlPage currentPage = (HtmlPage) webClient.getPage(new URL("http://www.google.com"));
//get list of all divs
final List<?> images = currentPage.getByXPath("//img");
for (Object imageObject : images) {
HtmlImage image = (HtmlImage) imageObject;
System.out.println(image.getSrcAttribute());
}
//webClient.closeAllWindows();
}
}

关于java - 如何从网页中抓取图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10099269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com