Java爬虫,获取未来40天预测气象并写入Excel

项目需要未来40天气象,找了一圈气象api,不是收费就是不支持未来40天,干脆写了个爬虫自动爬取气象网站的数据。以前都是用Python写的,重新拾起来再用Java写别有一番风味。

目标气象网站:西安天气预报40天_西安天气预报40天查询,西安未来40天天气预报- 东方天气

第三方依赖:

XML 复制代码
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.15.3</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.1.0</version>
</dependency>

代码:

java 复制代码
package com.test.main;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileOutputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class weatherSpider {
    public static void main(String[] args) {
        List<WeatherInfo> weatherInfos = new ArrayList<>();
        String url = "https://tianqi.eastday.com/xian/40/"; // 目标网页URL
        // 获取前两天的日期
        LocalDate twoDaysAgo = LocalDate.now().minusDays(2);
        // 设置日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");

        try {
            // 设置用户代理,尝试绕过访问限制
            Document document = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3")
                    .timeout(10000) // 设置超时时间
                    .get();

            // 查找所有class为temp的div标签
            Elements tempElements = document.select("div.temp");

            // 遍历所有找到的div标签
            for (Element tempElement : tempElements) {
                // 获取div标签内的所有span标签
                Elements spanElements = tempElement.select("span");

                // 遍历所有span标签并打印内容
                for (Element spanElement : spanElements) {
                    String spanText = spanElement.text();
                    System.out.println(spanText);
                    String lowTemp = spanText.split("~")[0];
                    String highTempC = spanText.split("~")[1];
                    String highTemp = highTempC.substring(0, highTempC.length() - 1);
                    System.out.println("日期:" + twoDaysAgo.format(formatter) + " 温度:" + lowTemp + " ~ " + highTemp);

                    WeatherInfo weatherInfo = new WeatherInfo();
                    weatherInfo.setDate(twoDaysAgo.format(formatter));
                    weatherInfo.setLowTemp(lowTemp); // 示例最低气温
                    weatherInfo.setHighTemp(highTemp); // 示例最高气温
                    weatherInfos.add(weatherInfo);
                    twoDaysAgo = twoDaysAgo.plusDays(1);
                    break;
                }

            }
            // 写入Excel
            writeWeatherToExcel(weatherInfos, "C:\\Users\\Xylon\\Desktop\\weather_forecast.xlsx");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // 将天气数据写入Excel
    private static void writeWeatherToExcel(List<WeatherInfo> weatherInfos, String filePath) throws Exception {
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("Weather Forecast");

            // 创建表头
            Row headerRow = sheet.createRow(0);
            String[] headers = {"Date", "LowTemp", "HighTemp"};
            for (int i = 0; i < headers.length; i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headers[i]);
            }

            // 写入天气数据
            for (int i = 0; i < weatherInfos.size(); i++) {
                Row row = sheet.createRow(i + 1);
                WeatherInfo weatherInfo = weatherInfos.get(i);
                row.createCell(0).setCellValue(weatherInfo.getDate());
                row.createCell(1).setCellValue(weatherInfo.getLowTemp());
                row.createCell(2).setCellValue(weatherInfo.getHighTemp());
            }

            // 写入文件
            try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
                workbook.write(fileOut);
            }
            System.out.println("Weather data has been written to " + filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 天气信息类
    @Setter
    @Getter
    static class WeatherInfo {
        private String date;
        private String lowTemp;
        private String highTemp;
    }

}
相关推荐
毕设源码-朱学姐43 分钟前
【开题答辩全过程】以 爱心捐赠网站为例,包含答辩的问题和答案
java·eclipse
尘觉3 小时前
中秋节与 Spring Boot 的思考:一场开箱即用的团圆盛宴
java·spring boot·后端
摩羯座-185690305943 小时前
爬坑 10 年!京东店铺全量商品接口实战开发:从分页优化、SKU 关联到数据完整性闭环
linux·网络·数据库·windows·爬虫·python
Le1Yu3 小时前
2025-10-7学习笔记
java·笔记·学习
popoxf3 小时前
spring容器启动流程(反射视角)
java·后端·spring
AAA修煤气灶刘哥4 小时前
监控摄像头?不,我们管这个叫优雅的埋点艺术!
java·后端·spring cloud
寻星探路5 小时前
Java EE初阶启程记09---多线程案例(2)
java·开发语言·java-ee
武子康5 小时前
Java-141 深入浅出 MySQL Spring事务失效的常见场景与解决方案详解(3)
java·数据库·mysql·spring·性能优化·系统架构·事务
珹洺5 小时前
Java-Spring入门指南(十五)SpringMVC注解开发
java·spring·microsoft
小满、5 小时前
什么是Maven?关于 Maven 的坐标、依赖管理与 Web 项目构建
java·maven