java小爬虫智联保存到excel
来源:互联网 发布:生命世界网络构图 编辑:程序博客网 时间:2024/04/29 23:36
import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CreationHelper;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Elements;import org.junit.Test;public class Jll2 {/** * *<p>Title:智联招聘网站爬虫小案例</p> *<p>Description:</p> *<p>Company:</p> *@author wty * @throws IOException *@date 2017年7月8日上午11:28:01 * */ public static String getHtmlCode(String url, String encoding) throws IOException { URL uri =null; URLConnection urlConnection =null; InputStream inputStream =null; InputStreamReader inputStreamReader = null; BufferedReader bReader =null; StringBuffer sBuffer= new StringBuffer(); try { // 建立网络连接 uri = new URL(url); // 打开连接 urlConnection = uri.openConnection(); //输入流 inputStream = urlConnection.getInputStream(); inputStreamReader = new InputStreamReader(inputStream, encoding); bReader = new BufferedReader(inputStreamReader); String temp; while ((temp = bReader.readLine()) != null) { sBuffer.append(temp + "\n"); } } catch (Exception e) { e.printStackTrace(); }finally { //关闭资源 if(bReader!=null){ try { bReader.close(); } catch (IOException e) { e.printStackTrace(); } } } return sBuffer.toString();} public static List<HashMap<String, String>> analyzeHtml(String url, String encoding) throws IOException{ String htmlCode = getHtmlCode(url, encoding); Document document = Jsoup.parse(htmlCode); Elements elements = document.getElementsByClass("newlist"); List<HashMap<String, String>> list=new ArrayList<>(); for (Element e : elements) { HashMap<String, String> map = new HashMap<>(); String zwmc = e.getElementsByClass("zwmc").text(); String gsmc = e.getElementsByClass("gsmc").text(); String zwyx = e.getElementsByClass("zwyx").text(); String gzdd = e.getElementsByClass("gzdd").text(); String gxsj = e.getElementsByClass("gxsj").text(); map.put("职位名称", zwmc); map.put("公司名称", gsmc); map.put("职位月薪", zwyx); map.put("工作地点", gzdd); map.put("发布日期", gxsj); list.add(map); } return list; } public static void main(String[] args) throws IOException { Workbook wb = new HSSFWorkbook();FileOutputStream fileOut = new FileOutputStream("E:\\fiveMonth\\poi\\txts2.xls");Sheet sheet = wb.createSheet("java"); List<HashMap<String, String>> resultList = analyzeHtml("http://sou.zhaopin.com/jobs/searchresult.ashx?jl=%E5%8C%97%E4%BA%AC&kw=java&p=1&isadv=0", "UTF-8"); for (int i = 0; i < resultList.size(); i++) {Row row = sheet.createRow(i);row.createCell(0).setCellValue(resultList.get(i).get("职位名称"));row.createCell(1).setCellValue(resultList.get(i).get("公司名称"));row.createCell(2).setCellValue(resultList.get(i).get("职位月薪"));row.createCell(3).setCellValue(resultList.get(i).get("工作地点"));row.createCell(4).setCellValue(resultList.get(i).get("发布日期"));} wb.write(fileOut);fileOut.close(); }}
阅读全文
0 0
- java小爬虫智联保存到excel
- hibernate多对多级联保存
- Hibernate的一对多配置中级联保存问题
- hibernate框架一对多级联保存例子(十二)
- 爬虫 爬内容存到txt中
- 「拉勾网」薪资调查的小爬虫,并将抓取结果保存到excel中
- 「拉勾网」薪资调查的小爬虫,并将抓取结果保存到excel中
- java小爬虫程序
- Java小爬虫Demo
- Java爬虫小程序
- python3 爬虫日记(二) 将数据存到Mongodb
- 知乎爬虫相关问题存到mongoDB中
- Java爬虫,爬取竞彩网数据并按指定格式输出到excel表格中
- hibernate一对多、多对多级联保存与级联删除与修改外键
- Java网络爬虫小试
- EXCEL批量复制小技巧,赶紧存起来!
- JAVA 序列化存/读小例子
- 读取Excel中的十六进制转化为图片 存到硬盘
- Android ExpandableListView实现仿好友列表
- STL的部分实现
- linux下shell script学习(一)
- 解决空指针异常的一些经验
- HDU 1024 (DP,有空在写一遍)
- java小爬虫智联保存到excel
- 前端富文本编辑器ckeditor的使用
- 列表及表单知识点
- css画圣诞树
- Android 数据存储方式(SQLite)
- 2017.7.08
- FP寄存器
- GT考试
- Spring入门之高级装配