使用Java来实现获取必应的每日壁纸(十分钟搞定,附源码) 您所在的位置:网站首页 必应每日壁纸怎么保存 使用Java来实现获取必应的每日壁纸(十分钟搞定,附源码)

使用Java来实现获取必应的每日壁纸(十分钟搞定,附源码)

2024-07-15 18:37| 来源: 网络整理| 查看: 265

今天我来教大家如何爬取必应的每日壁纸,十分钟搞定,源码在最后。

使用的技术

jsoup爬虫

IO流

工作步骤

获取必应每日壁纸的图片地址请求连接使用IO流下载图片并保存在桌面

开始操作

首先来获取图片地址:

打开必应的国内官网:https://cn.bing.com/?mkt=zh-CN按下F12,打开查找crtl+f,输入downloadLink,即可看到如下的网页结构(这里我已经帮大家定位到了图片链接的位置) image-20210821115328010 打开IDEA,新建maven工程,在pom.xml中导入jsoup依赖(这里只需要一个maven的空项目即可) org.jsoup jsoup 1.10.2 使用jsoup来获取网页document元素,并且定位到图片链接 public String getImgResource() throws IOException { //1.获取请求 String url = "https://cn.bing.com/?mkt=zh-CN"; //2.获取document对象(获取的就是javascript的页面对象),30秒内没有响应就失败 Document document = Jsoup.parse(new URL(url), 30000); //3.得到图片 Element vs_cont = document.getElementById("vs_cont"); Elements downloadLink = vs_cont.getElementsByClass("downloadLink"); String[] split = downloadLink.toString().split("\""); return "https://cn.bing.com/" + split[1]; }

接着来请求连接:

注意这里导入的URL包是import java.net.URL

//打开连接 URL url = new URL(imgResource); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //请求方式为GET connection.setRequestMethod("GET"); //超时相应时间10秒 connection.setConnectTimeout(10 * 1000);

最后来写IO流,下载图片:

//通过输入流获取图片 BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream()); //获取时间戳来给图片命名来避免重名,图片直接下载到桌面 File file = new File("C:\\Users\\pc\\Desktop\\" + System.currentTimeMillis() + ".jpg"); FileOutputStream outputStream = new FileOutputStream(file); //创建缓冲区 byte[] buffer = new byte[1024]; int len = 0; //写入图片 while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } //关闭流 inputStream.close(); outputStream.close();

点击运行项目,到这里就可以发现图片已经出现在桌面啦!

当然有的小伙伴可能不想每次打开IDEA来运行项目,这里可以导出jar包,每次下载壁纸来使用cmd的命令就可以运行项目了。

导出jar包可以参考这篇文章,教学链接:https://www.cnblogs.com/blog5277/p/5920560.html

导出完jar包后要注意这两个jar包要放在同一个目录下,否则会报错!!!

image-20210821120453712

最后打开cmd命令窗口输入:java -jar biyingPicture.jar就可以运行了

image-20210821120625852

源码

项目结构:

image-20210821120808898

GetImage类:

package com.zovz; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; import java.net.URL; /** * @author :zovz * @date :Created in 2021/8/21 10:20 * @description:获取图片 * @version: $1.0 */ public class GetImage { public String getImgResource() throws IOException { //1.获取请求 String url = "https://cn.bing.com/?mkt=zh-CN"; //2.获取document对象 Document document = Jsoup.parse(new URL(url), 30000); //3.得到图片 Element vs_cont = document.getElementById("vs_cont"); Elements downloadLink = vs_cont.getElementsByClass("downloadLink"); String[] split = downloadLink.toString().split("\""); return "https://cn.bing.com/" + split[1]; } }

downLoadImg类:

package com.zovz; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; /** * @author :zovz * @date :Created in 2021/8/21 11:14 * @description:下载图片 * @version: $1.0 */ public class downLoadImg { public static void main(String[] args) throws IOException { GetImage getImg = new GetImage(); String imgResource = getImg.getImgResource(); //打开连接 URL url = new URL(imgResource); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //请求方式为GET connection.setRequestMethod("GET"); //超时相应时间10秒 connection.setConnectTimeout(10 * 1000); //通过输入流获取图片 BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream()); //获取时间戳来给图片命名,避免重名 File file = new File("C:\\Users\\pc\\Desktop\\" + System.currentTimeMillis() + ".jpg"); FileOutputStream outputStream = new FileOutputStream(file); //创建缓冲区 byte[] buffer = new byte[1024]; int len = 0; //写入图片 while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } //关闭流 inputStream.close(); outputStream.close(); } }

pom.xml:

4.0.0 com.zovz biyingPicture 1.0-SNAPSHOT org.jsoup jsoup 1.10.2


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有