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

本文介绍了如何使用Java的jsoup库和IO流从必应网站抓取每日壁纸。通过分析网页结构,获取图片链接,然后利用HttpURLConnection下载图片,最后将图片保存到桌面。同时,提供了项目的源码结构和运行方法,包括如何导出jar包并在命令行运行。
摘要由CSDN通过智能技术生成

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

使用的技术

  1. jsoup爬虫

  2. IO流

工作步骤

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

开始操作

首先来获取图片地址:

  1. 打开必应的国内官网:https://cn.bing.com/?mkt=zh-CN
  2. 按下F12,打开查找crtl+f,输入downloadLink,即可看到如下的网页结构(这里我已经帮大家定位到了图片链接的位置)
image-20210821115328010
  1. 打开IDEA,新建maven工程,在pom.xml中导入jsoup依赖(这里只需要一个maven的空项目即可)
<dependencies>
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.10.2</version>
    </dependency>
</dependencies>
  1. 使用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];
}

接着来请求连接:

  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

<?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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值