java小爬虫智联保存到excel

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();
	    }
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于Python的网络爬虫使用了tkinter图形操作界面来增加毕业设计的工作量,并且目标网站是智联招聘。它可以通过爬取智联招聘网站获取各个职业的薪资、技能要求、工资地点等信息,并将这些信息转换为散点图和柱状图进行可视化展示。 该爬虫的工作原理是在爬虫界面预设目标网站的相关URL,并根据输入的不同信息对URL进行拼接,以获取相关的地址和信息。然后对获取的信息进行解析,从中提取需要的数据,并将这些数据创建并保存到对应的Excel表格中。接着,使用API从Excel中读取关键信息,并使用API生成可视化图像。 通过使用tkinter和网络爬虫技术,这个基于Python的爬虫可以帮助用户快速获取智联招聘网站上的职业信息,并通过可视化方式进行展示,从而方便用户进行分析和决策。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [毕业设计:基于Python的网络爬虫及数据处理(智联招聘)](https://download.csdn.net/download/weixin_54626591/85122394)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [基于Python的网络爬虫开发与实现](https://blog.csdn.net/sheziqiong/article/details/127546274)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值