idea
jsoup 依赖
一丢丢的java基础
第二步:找到我们需要的图片元素 对象 使用 css 选择器 jsoup已经为我们封装好了,一起来看吧
第三步:获取图片的网络路径
第四步:通过网络图片地址,将图片下载到本地
代码都在这里了 : public static void main(String [] args) throws IOException { Document doc = Jsoup.connect('http://www.baidu.com/').get(); Elements select = doc.select('.index-logo-src'); int i = 1; java.net.URL url = null; for (Element element : select) { String src = element.attr('src'); src = src.substring(2); src = 'http://' + src; url = new java.net.URL(src); DataInputStream dataInputStream = new DataInputStream(url.openStream()); FileOutputStream fileOutputStream = new FileOutputStream(new File('e:/img/' + (i) + '.gif')); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = dataInputStream.read(buffer)) > 0) { output.write(buffer, 0, length); } byte[] bytes = output.toByteArray(); fileOutputStream.write(output.toByteArray()); dataInputStream.close(); fileOutputStream.close(); i++; } }
如果对你有帮助的话就点个赞吧!