返回
揭秘 Selenium + POI 的 Excel 神器:批量查单词,轻松无忧!
见解分享
2024-02-14 17:10:17
对于语言学习者来说,查单词是家常便饭,而 Excel 又是一个办公必备工具,相信许多同学都曾幻想过能将 Excel 和查单词结合起来,实现自动化批量查单词的梦想。今天,我们就用 Selenium + POI 这对好搭档来帮你实现这个愿望!
Selenium:网页自动化利器
Selenium 是一款强大的网页自动化测试工具,它可以模拟浏览器行为,比如点击元素、输入文本、获取页面内容等。有了 Selenium,我们就可以自动完成浏览器中的查单词操作。
POI:Excel 读写专家
POI 是一个 Apache 开发的 Java 库,它可以方便地读写 Excel 文件。有了 POI,我们就可以将查到的单词结果写入 Excel,实现批量查单词的目的。
实战步骤
- 导入依赖库
首先,我们需要在项目中导入 Selenium 和 POI 的依赖库:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
- 启动 Selenium 浏览器
使用 Selenium 启动一个新的浏览器,并访问查单词网站:
WebDriver driver = new ChromeDriver();
driver.get("https://www.youdao.com/");
- 定位输入框和查询按钮
使用 Selenium 定位查单词输入框和查询按钮:
WebElement input = driver.findElement(By.id("query"));
WebElement submit = driver.findElement(By.id("submit"));
- 读取 Excel 单词
使用 POI 读取 Excel 文件中的单词:
Workbook workbook = WorkbookFactory.create(new File("words.xlsx"));
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
String word = row.getCell(0).toString();
// 这里可以对 word 进行处理,比如去除空格、转为小写等
}
- 自动查询单词
使用 Selenium 循环输入单词并查询:
for (String word : words) {
input.clear();
input.sendKeys(word);
submit.click();
// 这里可以获取查询结果,比如释义、例句等
}
- 写入 Excel 结果
使用 POI 将查询结果写入 Excel 文件:
Row row = sheet.createRow(index);
row.createCell(0).setCellValue(word);
row.createCell(1).setCellValue(result);
- 关闭浏览器
查询完成后,关闭 Selenium 浏览器:
driver.quit();
完整代码
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ExcelAutoSearchWord {
public static void main(String[] args) throws IOException {
// 启动 Selenium 浏览器
WebDriver driver = new ChromeDriver();
driver.get("https://www.youdao.com/");
// 定位输入框和查询按钮
WebElement input = driver.findElement(By.id("query"));
WebElement submit = driver.findElement(By.id("submit"));
// 读取 Excel 单词
List<String> words = new ArrayList<>();
Workbook workbook = WorkbookFactory.create(new File("words.xlsx"));
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
String word = row.getCell(0).toString();
words.add(word);
}
// 自动查询单词
for (String word : words) {
input.clear();
input.sendKeys(word);
submit.click();
// 这里可以获取查询结果,比如释义、例句等
String result = driver.findElement(By.className("trans-container")).getText();
// 写入 Excel 结果
Row row = sheet.createRow(index);
row.createCell(0).setCellValue(word);
row.createCell(1).setCellValue(result);
}
// 关闭浏览器
driver.quit();
// 保存 Excel 文件
FileOutputStream outputStream = new FileOutputStream("words.xlsx");
workbook.write(outputStream);
outputStream.close();
}
}
总结
通过使用 Selenium + POI,我们成功实现了一个自动化批量查单词的程序。这个程序可以帮助语言学习者高效地查单词,节省大量时间和精力。
附录
源码地址:https://github.com/your-github-username/excel-auto-search-word