今天我来教大家如何爬取必应的每日壁纸,十分钟搞定,源码在最后。
使用的技术
-
jsoup
爬虫 -
IO流
工作步骤
- 获取必应每日壁纸的图片地址
- 请求连接
- 使用IO流下载图片并保存在桌面
开始操作
首先来获取图片地址:
- 打开必应的国内官网:https://cn.bing.com/?mkt=zh-CN
- 按下F12,打开查找
crtl+f
,输入downloadLink
,即可看到如下的网页结构(这里我已经帮大家定位到了图片链接的位置)
- 打开IDEA,新建maven工程,在pom.xml中导入
jsoup
依赖(这里只需要一个maven的空项目即可)
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
</dependencies>
- 使用
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
包要放在同一个目录下,否则会报错!!!
最后打开cmd
命令窗口输入:java -jar biyingPicture.jar
就可以运行了
源码
项目结构:
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
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zovz</groupId>
<artifactId>biyingPicture</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
</dependencies>
</project>