Java 获取网站图片的示例代码
作者:曾桂花酒同少年游 时间:2022-03-11 14:52:44
前提
最近我的的朋友浏览一些网站,看到好看的图片,问我有没有办法不用手动一张一张保存图片!
我说用Jsoup丫!
测试网站
打开开发者模式(F12),找到对应图片的链接,在互联网中,每一张图片就是一个链接!
一、新建Maven项目,导入Jsoup环境依赖
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
二、代码编写
public class JsoupTest {
public static void main(String[] args) throws IOException {
// 爬虫的网站
String url="https://mp.weixin.qq.com/s/caU6d6ebpsLVJaf-7gMjtg";
// 获得网页的document对象
Document document = Jsoup.parse(new URL(url), 10000);
// 爬取含图片的代码部分
Element content = document.getElementById("js_content");
// 获取img标签代码 这是个集合
Elements imgs = content.getElementsByTag("img");
// 命名图片的id
int id=0;
for (Element img : imgs) {
// 获取具体的图片
String pic = img.attr("data-src");
URL target = new URL(pic);
// 获取连接对象
URLConnection urlConnection = target.openConnection();
// 获取输入流,用来读取图片信息
InputStream inputStream = urlConnection.getInputStream();
// 获取输出流 输出地址+文件名
id++;
FileOutputStream fileOutputStream = new FileOutputStream("E:\\JsoupPic\\" + id + ".png");
int len=0;
// 设置一个缓存区
byte[] buffer = new byte[1024 * 1024];
// 写出图片到E:\JsoupPic中, 输入流读数据到缓冲区中,并赋给len
while ((len=inputStream.read(buffer))>0){
// 参数一:图片数据 参数二:起始长度 参数三:终止长度
fileOutputStream.write(buffer, 0, len);
}
System.out.println(id+".png下载完毕");
// 关闭输入输出流 最后创建先关闭
fileOutputStream.close();
inputStream.close();
}
}
}
成果:
心得:
1、网络上的每一张图片都是一个链接
2、我们知道整个网页就是一个文档数,先找到包含图片的父id,再通过getElementsByTag()获取到图片的标签,通过F12,我们知道图片的链接是存在img标签里面的 data-src属性中
3、通过标签的data-src属性,就获取到具体图片的链接
4、通过输入输出流,把图片保存在本地中!
来源:https://blog.csdn.net/qq_44231797/article/details/121343892
标签:Java,获取,图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
kill命令在Java应用中使用的注意事项小结
2023-11-11 13:01:55
![](https://img.aspxhome.com/file/2023/9/58889_0s.jpg)
Java中的javaBean、vo、entity、domain和pojo
2023-03-30 21:41:20
![](https://img.aspxhome.com/file/2023/5/61605_0s.png)
剖析SpringCloud Feign中所隐藏的坑
2023-11-19 05:32:03
![](https://img.aspxhome.com/file/2023/4/59604_0s.png)
Java单例模式下的MongoDB数据库操作工具类
2023-11-20 12:55:01
使用eclipse创建java项目的方法
2022-10-15 04:41:00
![](https://img.aspxhome.com/file/2023/0/64010_0s.jpg)
Java maven三种仓库,本地仓库,私服,中央仓库的配置
2023-04-13 12:05:17
![](https://img.aspxhome.com/file/2023/2/62132_0s.png)
Jackson中json格式的字符串与对象的互相转换方式
2022-01-29 03:31:07
![](https://img.aspxhome.com/file/2023/8/67448_0s.png)
用Java连接sqlserver数据库时候几个jar包的区别分析
2021-11-07 02:18:01
c#开发cad预览图块步骤详解
2022-12-30 10:49:07
![](https://img.aspxhome.com/file/2023/7/67547_0s.jpg)
运行java的class文件方法详解
2021-07-29 03:53:48
Java 反射机制实例详解
2023-11-28 03:49:47
使用C#发送Http请求实现模拟登陆实例
2023-06-22 22:25:07
![](https://img.aspxhome.com/file/2023/7/67047_0s.png)
Java实现去除文档阴影的示例代码
2023-08-31 11:45:48
![](https://img.aspxhome.com/file/2023/5/58415_0s.png)
JAVA WSIMPORT生成WEBSERVICE客户端401认证过程图解
2023-11-14 00:27:55
![](https://img.aspxhome.com/file/2023/5/58985_0s.png)
java虚拟机内存溢出及泄漏实例
2023-11-27 15:22:06
解析Java设计模式编程中命令模式的使用
2023-11-12 04:49:45
![](https://img.aspxhome.com/file/2023/3/59513_0s.jpg)
Spring中多配置文件及引用其他bean的方式
2023-07-01 17:31:03
![](https://img.aspxhome.com/file/2023/5/62715_0s.jpg)
java泛型常用通配符实例解析
2022-09-16 22:14:50
![](https://img.aspxhome.com/file/2023/2/67472_0s.jpg)
JavaWeb实现文件上传下载功能实例详解
2023-05-08 19:43:51
![](https://img.aspxhome.com/file/2023/3/62633_0s.jpg)
Java Spring的refresh方法你知道吗
2023-07-08 11:53:18
![](https://img.aspxhome.com/file/2023/5/67095_0s.png)