jsoup 对网页中图片解析

时间:2023-03-09 22:53:38
jsoup 对网页中图片解析

Elements article = new Elements();

Elements Img = new Elements();

article = doc.select("div#contentText");

Img = article.first().select("img");

for (Element img : Img) {

url = img.attr("src"); // 获取图片的url
img.append("[[]]");   // 会对Element对象进行修改
DownLoad(url);   // 下载图片
}

void DownLoad(String url, File path) throws IOException {

URL url = new URL(uri);
InputStream is = url.openConnection().getInputStream();
FileOutputStream os = new FileOutputStream(path);

int i = 0;
while ((i = is.read()) != -1) {
os.write(i);
}

is.close();
os.close();

}