代码拉取完成,页面将自动刷新
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
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.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
public class Main1 {
public static void main(String[] args) throws IOException, InvalidFormatException {
// Load the Excel file
String excelFilePath = "E:\\desktop\\ziku\\bushoubihua.xlsx";
Workbook workbook = new XSSFWorkbook(new File(excelFilePath));
Sheet sheet = workbook.getSheetAt(0); // assuming you want the first sheet
int index = 0;
for (Row row : sheet) {
Cell cell = row.getCell(0); // get the first column
String keyword = cell.getStringCellValue();
// Replace the keyword in the URL
String url = "https://zd.hwxnet.com/search.do?keyword=" + keyword;
Document document = Jsoup.parse(new URL(url),30000);
Elements elements = document.getElementsByClass("spwid80");
Elements elements1 = document.getElementsByClass("clearfix");
Elements elements2 = document.getElementsByClass("spwid120");
// Add the obtained text to the second column of the current row in the Excel file
Cell outputCell = row.createCell(1);
//部首
if(elements2.get(0).text().equals("通用字")||elements2.get(0).text().equals("通用字、常用字")||elements2.get(0).text().equals("通用字、次常用字")){
outputCell.setCellValue(elements.get(0).text());
//总笔画
Cell outputCell1 = row.createCell(2);
outputCell1.setCellValue(elements.get(2).text());
//拼音
Cell outputCell2 = row.createCell(3);
String html = elements1.toString();
Document parse = Jsoup.parse(html);
Element introduce1 = parse.getElementsByClass("introduce").first();
// 遍历所有的"introduce"元素
if (introduce1 != null){
// 获取每个"introduce"元素下的"label"元素
Elements labels = introduce1.getElementsByClass("label");
// 检查是否有足够多的"label"元素
if (labels.size() > 1) {
// 获取第二个"label"元素
Element secondLabel = labels.get(1);
// 获取该"label"元素下所有的"span"元素
Elements spans = secondLabel.getElementsByTag("span");
// 遍历所有的"span"元素并打印其内容
String pinyin = "";
for (Element span : spans) {
pinyin+= span.text()+"\t";
}
outputCell2.setCellValue(pinyin);
}
}
// Print a message to the console
System.out.println("Successfully parsed and wrote keyword: " + keyword);
}else {
index++;
// System.out.println(keyword);
}
// System.out.println("抛出"+index+"个");
}
// Save the changes to a new file
String outputExcelFilePath = "E:\\desktop\\ziku\\bushoubihua001.xlsx";
try (FileOutputStream outputStream = new FileOutputStream(outputExcelFilePath)) {
workbook.write(outputStream);
}
workbook.close();
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。