我们已经成功抓取了天气、新闻和商品信息,接下来,我们使用 Apache POI 将数据存入 Excel,方便后续分析。

引入 Maven 依赖
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.3</version>
</dependency>

一、模拟存储天气数据到 Excel

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelWriter {
    public static void main(String[] args) {
        try (Workbook workbook = new XSSFWorkbook();
             FileOutputStream fileOut = new FileOutputStream("weather.xlsx")) {

            Sheet sheet = workbook.createSheet("天气数据");
            Row row = sheet.createRow(0);
            row.createCell(0).setCellValue("城市");
            row.createCell(1).setCellValue("天气");
            row.createCell(2).setCellValue("温度");

            Row dataRow = sheet.createRow(1);
            dataRow.createCell(0).setCellValue("北京");
            dataRow.createCell(1).setCellValue("晴天");
            dataRow.createCell(2).setCellValue("15°C");

            workbook.write(fileOut);
            System.out.println("天气数据已保存至 weather.xlsx");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
执行结果

在本地生成 weather.xlsx 文件,里面包含天气数据。

二、实际案例:结合淘宝爬虫,将商品数据存入Excel 中

直接询问Deepseek:结合淘宝爬虫案例,将爬到的数据存入到 Excel 中。

生成的代码如下:

public class TaobaoScraperData2Excel {
    public static void main(String[] args) throws InterruptedException {
        // WebDriverManager 自动下载并管理 ChromeDriver
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.get("https://s.taobao.com/search?page=1&q=iPhone16pro&tab=all");
        // 等待页面加载(可以用显式等待替代)
        Thread.sleep(3000);
        // 获取所有商品的 DOM 元素
        List<WebElement> items = driver.findElements(By.cssSelector(".doubleCard--gO3Bz6bu"));
        // 记录商品信息,方便存储到Excel 中
        List<Map<String,String>> productList = new ArrayList<>();
        for (WebElement item : items) {
            try {
                // 商品标题
                String title = item.findElement(By.cssSelector(".title--qJ7Xg_90 span")).getText();
                // 商品价格
                String price = item.findElement(By.cssSelector(".innerPriceWrapper--aAJhHXD4")).getText();
                // 付款人数
                String sales = item.findElement(By.cssSelector(".realSales--XZJiepmt")).getText();
                // 商品链接
                String link = item.findElement(By.tagName("a")).getAttribute("href");
                // 打印商品信息
                Map<String,String> product = new HashedMap<>();
                product.put("标题",title);
                product.put("价格",price);
                product.put("销量",sales);
                product.put("链接",link);
                productList.add(product);
            } catch (Exception e) {
                //System.out.println("获取商品信息失败: " + e.getMessage());
            }
        }
        // 将产品数据导入到Excel 中
        product2Excel(productList);
        // 关闭浏览器
        driver.quit();
    }

    public static void product2Excel(List<Map<String,String>> productList){
        try (Workbook workbook = new XSSFWorkbook();
             FileOutputStream fileOut = new FileOutputStream("IPhone16Pro商品.xlsx")) {

            Sheet sheet = workbook.createSheet("淘宝IPhonePro 商品数据");
            Row row = sheet.createRow(0);
            row.createCell(0).setCellValue("标题");
            row.createCell(1).setCellValue("价格");
            row.createCell(2).setCellValue("销量");
            row.createCell(3).setCellValue("链接");

            for (int i = 0; i < productList.size(); i++) {
                Map<String, String> productInfo = productList.get(i);
                Row dataRow = sheet.createRow(i+1);
                dataRow.createCell(0).setCellValue(productInfo.get("标题"));
                dataRow.createCell(1).setCellValue(productInfo.get("价格"));
                dataRow.createCell(2).setCellValue(productInfo.get("销量"));
                dataRow.createCell(3).setCellValue(productInfo.get("链接"));
            }

            workbook.write(fileOut);
            System.out.println("IPhone16Pro商品 数据已保存至 IPhone16Pro商品.xlsx");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行后,生成一个 IPhone16Pro商品.xlsx 文件,打开文件,效果如下:

image-nlfKxZWIeKdITnqkKCWomZypnuTpJpZV.png


总结

💡 现在你已经学会了 用 Java 实现爬虫、抓取数据、生成 Excel 报告!🚀
想要扩展功能?可以尝试询问DeepSeek 抓取更多网站数据,或者将数据存入数据库!