java从网页抓取数据(从一个网页下载数据到Excel文件中的搜狗指数-宠物-源码接下来 )

优采云 发布时间: 2021-12-25 07:20

  java从网页抓取数据(从一个网页下载数据到Excel文件中的搜狗指数-宠物-源码接下来

)

  在处理数据分析时,如果我们的数据来自一个网页,我们需要使用数据爬虫将网页上的数据转换到本地存储,然后进行分析。

  在这个文章中,我们将逐步介绍从搜狗指数下载数据到Excel文件。

  分析搜狗指数页面

  如果要从网页中下载数据,首先需要了解网页中的数据结构。这里我们以宠物搜索数据为例。

  对于这样的页面数据

  

  搜狗指数-宠物

  通过F12输入网页的源代码,可以看到

  

  搜狗指数-宠物-源码

  接下来,我们刚刚想出了一个方法,将脚本标签中的数据导出

  通过Java将搜狗指数数据保存到Excel

  Pattern p = Pattern.compile("root.SG.wholedata = (.*)", Pattern.MULTILINE);

Matcher matcher = p.matcher(script);

String wholedata = "";

while(matcher.find()) {

wholedata = matcher.group(1);

}

ObjectMapper objectMapper = new ObjectMapper();

SougouData sougouData = objectMapper.readValue(wholedata, SougouData.class);

for (Pv pv : sougouData.pvList.get(0)) {

IndexTrend indexTrend = new IndexTrend();

indexTrend.date = pv.date;

indexTrend.PV = pv.pv;

indexTrends.add(indexTrend);

}

  public void SaveToExcel(Set indexTrends) throws IOException {

XSSFWorkbook workbook = new XSSFWorkbook();

XSSFSheet sheet = workbook.createSheet("Pet");

int rowNum = 0;

for (IndexTrend indexTrend : indexTrends) {

Row row = sheet.createRow(rowNum++);

Cell date = row.createCell(0);

Cell pv = row.createCell(1);

CellStyle cellStyle = workbook.createCellStyle();

CreationHelper createHelper = workbook.getCreationHelper();

cellStyle.setDataFormat(

createHelper.createDataFormat().getFormat("yyyy-MM-dd"));

date.setCellValue(indexTrend.date);

date.setCellStyle(cellStyle);

pv.setCellValue(indexTrend.PV);

}

FileOutputStream outputStream = new FileOutputStream("./Download Sougou To Excel/petdata.xlsx");

workbook.write(outputStream);

workbook.close();

}

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线